How to test a getter and setter in Jest (Angular)

Discussão em 'Angular' iniciado por Babulaas, Janeiro 23, 2025.

  1. Babulaas

    Babulaas Guest

    I am trying to test a getter / setter value after it change. All test are fine except the last one in comments.

    What can I do to make this an successful test. Do I need use an Spy or something else to achieve this. Can someone point me in the right direction

    Also can must I update the other tests with kind of spy or mockdata.

    TS:

    export class component001 {
    isLastRecord = false;

    private _records: Records[];
    @Input()
    set records(value: Record[]){
    if(value) {
    this._records = value;
    this.isLastRecord = value.filter(r => r.type === 'type001').length === 1;
    }
    }
    get records(): Record[] {
    return this._records;
    }
    }


    Jest:

    describe("component001", () => {
    let component: Component001;
    let fixture: ComponentFixture<Component001>;

    beforeEach(async () => {
    await TestBed.configureTestingModule({
    imports: [Component001, HttpClientTestingModule]
    }).compileComponents();
    });

    beforeEach(() => {
    fixture = TestBed.createComponent(Component001);
    component = fixture.componentInstance;
    fixture.detectChanges();
    });

    describe('Component001', () => {
    it('Should return true when there the count of record changes from 2 to 1 record', () => {
    let mockRecords = [
    { name: 'Barbara Gordon', type: 'type001' },
    { name: 'Cassandra Cain', type: 'type001'}
    ] as unknown as Record [];

    expect(component.isLastRecord).toBe(false);
    component.dnsRecords = mockRecords;
    expect(component.isLastRecord).toBe(false);
    component.dnsRecords = [mockRecords.pop()];
    expect(component.isLastRecord).toBe(true);
    });

    // it('Should return true when there the count of record changes from 1 to 2 records', () => {
    // let mockRecords = [
    // { name: 'Barbara Gordon', type: 'type001' }
    // ] as unknown as Record [];

    // expect(component.isLastRecord).toBe(false);
    // component.Records = mockRecords;
    // expect(component.isLastRecord).toBe(true);

    // const newRecord = {name: 'Cassandra Cain', type: 'type001'} as unknown as Record;
    // component.records.push(newRecord);
    // expect(component.isLastRecord).toBe(false);
    // });

    });
    })


    Also I am using angular 16 with standalone components

    Continue reading...

Compartilhe esta Página