How to test a prop update on React component

AirBnB’s Enzyme library provides an elegant solution to this question. it provides a setProps method, that can be called on either a shallow or jsdom wrapper. it(“Component should call componentWillReceiveProps on update”, () => { const spy = sinon.spy(Component.prototype, “componentWillReceiveProps”); const wrapper = shallow(<Component {…props} />); expect(spy.calledOnce).to.equal(false); wrapper.setProps({ prop: 2 }); expect(spy.calledOnce).to.equal(true); });

How can I run tests with a headless browser?

As a more complete answer based on William Hampshire‘s one, Cuga‘s comment and my personal additions. Short answer: using ChromeHeadless You can just use Headless Chrome: ng test –browsers ChromeHeadless You need to have Chrome 59+. But if you need PhantomJS (and/or chaning the default ng test behaviour with no arguments) read the following. Longer … Read more

Expect not toThrow function with arguments – Jasmine

toThrow matcher requires function to be passed as argument to expect so you can simply wrap your function call in anonymous function: expect(function() { myFunc(arg1, arg2, arg3); }).not.toThrow(); You can also use bind to create new ‘version’ of your function that when called will be passed provided arguments: expect(myFunc.bind(null, arg1, arg2, arg3)).not.toThrow();

jasmine: spyOn(obj, ‘method’).andCallFake or and.callFake?

Yes, the spy API changed from Jasmine 1.3.1 to Jasmine 2.0. There’s no “correct” version. If you can find the tool support for Jasmine 2.0, I would recommend upgrading. Jasmine 1.3.1 syntax (documentation [archived link]) spyOn(mBankAccountResource, ‘getBankAccountData’).andCallFake(fakedFunction); expect(mBankAccountResource.getBankAccountData.mostRecentCall.args).toEqual([“foo”]); Jasmine 2.0 syntax (documentation [archived link]) // Methods moved to ‘and’ property spyOn(mBankAccountResource, ‘getBankAccountData’).and.callFake(fakedFunction); // Call data … Read more

Updating input html field from within an Angular 2 test

You’re right that you can’t just set the input, you also need to dispatch the ‘input’ event. Here is a function I wrote earlier this evening to input text: function sendInput(text: string) { inputElement.value = text; inputElement.dispatchEvent(new Event(‘input’)); fixture.detectChanges(); return fixture.whenStable(); } Here fixture is the ComponentFixture and inputElement is the relevant HTTPInputElement from the … Read more

MatDialog Service Unit Test Angular 6 Error

Testing mat-dialogs can be tricky. I tend to use a spy object for the return from a dialog open (dialogRefSpyObj below) so I can more easily track and control tests. In your case it might look something like the following: describe(‘ModalService’, () => { let modalService: ModalService; let dialogSpy: jasmine.Spy; let dialogRefSpyObj = jasmine.createSpyObj({ afterClosed … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)