Last active
February 21, 2020 11:19
-
-
Save ali-kamalizade/fb88e9e70cc4de181cc089c5f74b8966 to your computer and use it in GitHub Desktop.
Testing a custom Angular Form Control with Jest / Jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
describe('JsonFormControlComponent', () => { | |
let fixture: ComponentFixture < JsonFormControlComponent > ; | |
let emitValueSpy: jasmine.Spy; | |
beforeEach(() => | |
TestBed.configureTestingModule({ | |
declarations: [JsonFormControlComponent], // the component we want to test | |
schemas: [NO_ERRORS_SCHEMA] // optional: ignore other custom elements | |
}) | |
); | |
beforeEach(() => { | |
fixture = TestBed.createComponent(JsonFormControlComponent); | |
emitValueSpy = spyOn(fixture.componentInstance, 'propagateChange').and.callThrough(); | |
const currentValue = { | |
id: '123', | |
email: '[email protected]' | |
}; | |
fixture.componentInstance.writeValue(currentValue); | |
fixture.detectChanges(); | |
}); | |
it('emits the new value if it is valid JSON', fakeAsync(() => { | |
fixture.componentInstance.editorFormControl.setValue(JSON.stringify({ | |
active: false | |
})); | |
tick(300); | |
expect(emitValueSpy).toHaveBeenCalledWith({ | |
active: false | |
}); | |
})); | |
it('does not emit the new value if it is invalid JSON', fakeAsync(() => { | |
fixture.componentInstance.editorFormControl.setValue({ | |
active: false | |
}); | |
tick(300); | |
expect(emitValueSpy).not.toHaveBeenCalled(); | |
})); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment