Skip to content

Commit

Permalink
BW-805 #comment unit test for check display name and invite mail frie…
Browse files Browse the repository at this point in the history
…nd component
  • Loading branch information
jitendraashutec committed Jun 19, 2020
1 parent c75d9f1 commit 1b5f9fe
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 27 deletions.
4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module.exports = function (config) {


// list of files / patterns to load in the browser
files: ['projects/trivia/src/tests/**/*.ts'],
files: [
'projects/trivia/src/tests/setup.ts',
'projects/trivia/src/tests/**/*.ts'],

// list of files to exclude
exclude: [
Expand Down
96 changes: 72 additions & 24 deletions projects/trivia/src/tests/check-display-name.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import 'reflect-metadata';
// tslint:disable-next-line: max-line-length
import { CheckDisplayNameComponent } from '../../../shared-library/src/lib/shared/components/check-display-name/check-display-name.component.tns';
import { Utils } from './../../../shared-library/src/lib/core/services/utils';
import { CheckDisplayNameComponent } from 'shared-library/shared/components/check-display-name/check-display-name.component';
import { Utils } from 'shared-library/core/services/utils';
import { ElementRef } from '@angular/core';
import { nsTestBedBeforeEach, nsTestBedAfterEach, nsTestBedRender, nsTestBedInit } from 'nativescript-angular/testing';
import { nsTestBedBeforeEach, nsTestBedAfterEach, nsTestBedRender } from 'nativescript-angular/testing';
import { ComponentFixture } from '@angular/core/testing';

describe('CheckDisplayNameComponent', () => {
beforeAll(() => nsTestBedInit());

let component: CheckDisplayNameComponent;
let fixture: ComponentFixture<CheckDisplayNameComponent>;
let spy: any;
const event = 'new text change';

afterAll(() => { });
beforeEach(nsTestBedBeforeEach([CheckDisplayNameComponent], [
{
Expand All @@ -19,30 +24,73 @@ describe('CheckDisplayNameComponent', () => {
}
]));
afterEach(nsTestBedAfterEach());


beforeEach((async () => {
fixture = await nsTestBedRender(CheckDisplayNameComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));

it('should create', () => {
return nsTestBedRender(CheckDisplayNameComponent).then((fixture) => {
const component = fixture.componentInstance;
expect(component).toBeTruthy();
});
expect(component).toBeTruthy();
});

it('on onChanges call it should set focus on textbox', () => {
return nsTestBedRender(CheckDisplayNameComponent).then((fixture) => {
const component = fixture.componentInstance;
component.textField = new ElementRef('any');
const spy = spyOn(component.utils, 'focusTextField').and.callThrough();
expect(spy);
component.ngOnChanges({
disabled:
{
previousValue: undefined,
currentValue: true,
firstChange: true,
isFirstChange: undefined
}
});
expect(component.utils.focusTextField).toHaveBeenCalled();
component.textField = new ElementRef('any');
spy = spyOn(component.utils, 'focusTextField').and.callThrough();
expect(spy);
component.ngOnChanges({
disabled:
{
previousValue: undefined,
currentValue: true,
firstChange: true,
isFirstChange: undefined
}
});
expect(component.utils.focusTextField).toHaveBeenCalled();
});

it('Initial value of the myValue should be falsy and propagateChange should be truthy', () => {
expect(component.propagateChange).toBeTruthy();
expect(component.myValue).toBeFalsy();
});

it(`it should assign set disabled is false if isProfilePage is false`, () => {
component.isProfilePage = false;
component.ngOnInit();
expect(component.disabled).toEqual(false);
});

it(`it should assign set disabled is true if isProfilePage is true`, () => {
component.isProfilePage = true;
component.ngOnInit();
expect(component.disabled).toEqual(true);
});

it(`call to writeValue function should set the input text`, () => {
component.writeValue(event);
expect(component.myValue).toEqual(event);
});

it(`call to onTextChange function should emit the propagateChange event`, () => {
spy = spyOn(component, 'propagateChange').and.callThrough();
expect(spy);
component.onTextChange(event);
expect(component.propagateChange).toHaveBeenCalled();
});

it(`call to registerOnChange function should emit the propagateChange event`, () => {
spy = spyOn(component, 'propagateChange').and.callThrough();
expect(spy);
component.registerOnChange(event);
expect(component.propagateChange).toBe(event);
});

it(`call to setDisabledState and change the input disabled based on argument received`, () => {
component.setDisabledState(true);
expect(component.disabled).toEqual(true);
});

});
4 changes: 2 additions & 2 deletions projects/trivia/src/tests/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// A sample Jasmine test
describe('A suite', () => {
it('containsss spec with asdsn expectation', () => {
describe('A suite', () => {
it('contain spec with as expectation', () => {
expect(true).toBe(true);
});

Expand Down
213 changes: 213 additions & 0 deletions projects/trivia/src/tests/invite-mail-friends.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import 'reflect-metadata';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {
nsTestBedAfterEach,
nsTestBedBeforeEach,
nsTestBedRender,
} from 'nativescript-angular/testing';
import { InviteMailFriendsComponent } from 'shared-library/shared/components/invite-mail-friends/invite-mail-friends.component';
import { NativeScriptFormsModule } from 'nativescript-angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { StoreModule, MemoizedSelector, Store } from '@ngrx/store';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { coreState, CoreState, UserActions, ActionWithPayload } from 'shared-library/core/store';
import { Utils } from 'shared-library/core/services';
import { testData } from 'test/data';

describe('InviteMailFriendsComponent', () => {
let component: InviteMailFriendsComponent;
let fixture: ComponentFixture<InviteMailFriendsComponent>;
let mockStore: MockStore<CoreState>;
let spy: any;
let mockCoreSelector: MemoizedSelector<CoreState, Partial<CoreState>>;
afterEach(nsTestBedAfterEach());
beforeEach(nsTestBedBeforeEach (
[InviteMailFriendsComponent],
[UserActions,
{
provide: Utils,
useValue: {
hideKeyboard() {
return '';
}
}
},
provideMockStore({
initialState: {},
selectors: [
{
selector: coreState,
value: {}
}
]
})
],
[ReactiveFormsModule, NativeScriptFormsModule, StoreModule.forRoot({})]
));
beforeEach((async () => {
fixture = await nsTestBedRender(InviteMailFriendsComponent);
mockStore = TestBed.get(Store);
component = fixture.componentInstance;
mockCoreSelector = mockStore.overrideSelector<CoreState, Partial<CoreState>>(coreState, {});
spy = spyOn(mockStore, 'dispatch');
fixture.detectChanges();
}));

it('should create', () => {
expect(component).toBeTruthy();
});

it('Form should be default invalid', () => {

const spy = spyOn(component.utils, 'hideKeyboard').and.callThrough();
expect(spy);
component.hideKeyboard();
expect(component.utils.hideKeyboard).toHaveBeenCalled();

expect(component.invitationForm.valid).toBeFalsy();
});

it('on load component should have empty message', () => {
expect(component.errorMsg).toEqual('');
});

it('on load component should not show error message', () => {
component.invitationForm.get('email').setValue('[email protected]');
expect(component.showErrorMsg).toBeFalsy();
});

it(`on load component user should set`, () => {
mockCoreSelector.setResult({ user: testData.userList[0] });
mockStore.refreshState();
expect(component.user).toEqual(testData.userList[0]);
});

it('on load component should set applicationSettings', () => {
mockCoreSelector.setResult({ applicationSettings: [testData.applicationSettings] });
mockStore.refreshState();
expect(component.applicationSettings).toEqual(testData.applicationSettings);
});

it('Form should have email required error when email is not set', () => {
expect(component.invitationForm.get('email').errors).toEqual({ 'required': true });
});

it('Form should be valid when valid email is provided', () => {
component.invitationForm.get('email').setValue('[email protected]');
expect(component.invitationForm.valid).toBeTruthy();
});


it('Check isValid will return true if email is valid', () => {
expect(component.isValid('[email protected]')).toBeTruthy();
});

it(`Email value 'test' should make isValid function to return false`, () => {
expect(component.isValid('test')).toBeFalsy();
});

it(`Email value 'test@test' should make isValid function to return false`, () => {
expect(component.isValid('test@test')).toBeFalsy();
});

it(`Email value 'test@test.' should make isValid function to return false`, () => {
expect(component.isValid('test@test.')).toBeFalsy();
});

it(`Error message should be set when wrong email is entered`, () => {
component.invitationForm.controls['email'].setValue('test@test');
component.onSubscribe();
expect(component.errorMsg).toBe('Following email is not valid address!');
});


// tslint:disable-next-line: max-line-length
it(`on Subscribe check if email is invalid then invalid email will add into invalid email list and show error msg should be false `, () => {
component.invitationForm.controls['email'].setValue('test@test');
component.onSubscribe();
expect(component.showErrorMsg).toBeTruthy();
expect(component.invalidEmailList.length).toEqual(1);
});

it(`on Subscribe errorMsg message should be set if wrong multiple email inserted `, () => {
component.invitationForm.controls['email'].setValue('test@test,second.mail');
component.onSubscribe();
expect(component.errorMsg).toBe('Following emails are not valid address!');
});


it(`on Subscribe show error message should be true set if invalid single email inserted`, () => {
component.invitationForm.controls['email'].setValue('test@test,second.mail');
component.onSubscribe();
expect(component.showErrorMsg).toBeTruthy();
expect(component.invalidEmailList.length).toEqual(2);
});


it(`on Subscribe invalid email should push on invalidEmailList list should 1`, () => {
component.invitationForm.controls['email'].setValue('test@test,[email protected]');
component.onSubscribe();
expect(component.invalidEmailList.length).toEqual(1);
});

it(`on Subscribe valid email should push on validEmail list length should be 2`, () => {
component.invitationForm.controls['email'].setValue('test@test,[email protected],[email protected]');
component.onSubscribe();
expect(component.validEmail.length).toEqual(2);
});

it(`on Subscribe should dispatch action to add user invitation with correct payload `, () => {
component.invitationForm.controls['email'].setValue('[email protected]');
const user = { ...testData.userList[0] };
mockCoreSelector.setResult({ user });
mockStore.refreshState();

const payloadData = {
userId: user.userId,
emails: ['[email protected]']
};

spy.and.callFake((action: ActionWithPayload<string>) => {
expect(action.type).toEqual(UserActions.ADD_USER_INVITATION);
expect(action.payload).toEqual(payloadData);
});

component.onSubscribe();
expect(mockStore.dispatch).toHaveBeenCalled();
});

it(`Invitation message should set to showSuccessMsg`, () => {
const userProfileSaveStatus = 'Invitation is sent on [email protected]';
mockCoreSelector.setResult({ userProfileSaveStatus });
mockStore.refreshState();
expect(component.showSuccessMsg).toEqual(userProfileSaveStatus);
});

it(`Invitation message should not set to showSuccessMsg if it status is 'NONE'`, () => {
const userProfileSaveStatus = 'NONE';
mockCoreSelector.setResult({ userProfileSaveStatus });
mockStore.refreshState();
expect(component.showSuccessMsg).toBeUndefined();
});

it(`Invitation message should not set to showSuccessMsg if it status is 'IN PROCESS'`, () => {
const userProfileSaveStatus = 'IN PROCESS';
mockCoreSelector.setResult({ userProfileSaveStatus });
mockStore.refreshState();
expect(component.showSuccessMsg).toBeUndefined();
});

it(`Invitation message should not set to showSuccessMsg if it status is 'SUCCESS'`, () => {
const userProfileSaveStatus = 'SUCCESS';
mockCoreSelector.setResult({ userProfileSaveStatus });
mockStore.refreshState();
expect(component.showSuccessMsg).toBeUndefined();
});

it(`Invitation message should not set to showSuccessMsg if it status is 'MAKE FRIEND SUCCESS'`, () => {
const userProfileSaveStatus = 'MAKE FRIEND SUCCESS';
mockCoreSelector.setResult({ userProfileSaveStatus });
mockStore.refreshState();
expect(component.showSuccessMsg).toBeUndefined();
});
});
3 changes: 3 additions & 0 deletions projects/trivia/src/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'nativescript-angular/zone-js/testing.jasmine';
import {nsTestBedInit} from 'nativescript-angular/testing';
nsTestBedInit();
1 change: 1 addition & 0 deletions tsconfig.tns.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"shared-library/*": [
"./projects/shared-library/src/lib/*"
],
"test/*": ["./test/*"],
"*": [
"./node_modules/tns-core-modules/*",
"./node_modules/*"
Expand Down

0 comments on commit 1b5f9fe

Please sign in to comment.