Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing Component form submit empty values #187

Closed
ekoome opened this issue Dec 3, 2016 · 4 comments
Closed

Unit testing Component form submit empty values #187

ekoome opened this issue Dec 3, 2016 · 4 comments

Comments

@ekoome
Copy link

ekoome commented Dec 3, 2016

I'm testing a simple component as a proof of concept, however i have struggled to get the form to submit with set values for the input. On submit, the action function is called with empty values. Not sure what i'm missing. Tried various permuattions of dispatching input event, using the TestUtils.EventFire - which doesn't fire the submit function.

Here's the component class:

`export class PassCode {

pincode: number;

constructor(public navCtrl: NavController) {

}

logForm(value: any): number {

return value.first_digit +value.second_digit + value.third_digit + value.fourth_digit;// dummy return 

}

}`

Here's the form:

<form #form="ngForm" (ngSubmit)="logForm(form.value)"> <div class="pincontainer"> <div class="pinpad"> <input type="number" name='first_digit' ngModel required maxlength="1"> <input type="number" name='second_digit' ngModel required maxlength="1"> <input type="number" name='third_digit' ngModel required maxlength="1"> <input type="number" name='fourth_digit' ngModel required maxlength="1"> </div> <div padding> <button ion-button color="primary" block type="submit">Verify Number</button> </div> </div> </form>

Here's the unit test:

`import { ComponentFixture, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { dispatchEvent } from '@angular/platform-browser/testing/browser_util';
import { TestUtils } from '../../test';
import { PassCode } from './passcode.component';

let fixture: ComponentFixture = null;
let instance: any = null;
let firstInput: any;
let secondInput: any;
let thirdInput: any;
let fourthInput: any;

describe('Pages: PassCode', () => {

beforeEach(async(() => TestUtils.beforeEachCompiler([PassCode]).then(compiled => {
fixture = compiled.fixture;
instance = compiled.instance;
})));

it('should create the Passcode page', () => {
const element = fixture.debugElement.nativeElement;
//spyOn(instance, 'logForm').and.callThrough();
spyOn(instance, 'logForm');

fixture.detectChanges();

//console.log(element);
firstInput = element.querySelector('input[name=first_digit]');
firstInput.value = 1;
dispatchEvent(firstInput, 'input');

secondInput = element.querySelector('input[name=second_digit]');
secondInput.value = 2;
dispatchEvent(secondInput, 'input');

thirdInput = element.querySelector('input[name=third_digit]');
thirdInput.value = 3;
dispatchEvent(thirdInput, 'input');

fourthInput = element.querySelector('input[name=fourth_digit]');
fourthInput.value = 4;
dispatchEvent(fourthInput, 'input');


fixture.detectChanges();

//TestUtils.eventFire(element.querySelector('button[type=submit]'), 'click');
element.querySelector('button[type=submit]').click();

expect(instance.logForm).toHaveBeenCalledWith(1); //this is dummy to check what is submitted

});
});`

@lathonez
Copy link
Owner

lathonez commented Dec 4, 2016 via email

@ekoome
Copy link
Author

ekoome commented Dec 4, 2016

In case someone has similar issues, i picked this up from deep recesses of angular changelog - goodness me this will make mw grow old!

Since ngModel is now asynchronous, you have to use fixture.whenStable() as mentioned here angular/angular#10148. I also had to add jasmine done() to the test to deal with the asynchronous test.

Hope this helps.

@lathonez
Copy link
Owner

lathonez commented Dec 8, 2016

@ekoome - thanks for posting the solution

@lathonez lathonez closed this as completed Dec 8, 2016
@lathonez
Copy link
Owner

#191

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants