From 1b5f9fe5d995a53e547d2702543651fba9ebcd56 Mon Sep 17 00:00:00 2001 From: jitendraashutec Date: Fri, 19 Jun 2020 09:54:49 +0530 Subject: [PATCH] BW-805 #comment unit test for check display name and invite mail friend component --- karma.conf.js | 4 +- .../src/tests/check-display-name.component.ts | 96 ++++++-- projects/trivia/src/tests/example.ts | 4 +- .../tests/invite-mail-friends.component.ts | 213 ++++++++++++++++++ projects/trivia/src/tests/setup.ts | 3 + tsconfig.tns.json | 1 + 6 files changed, 294 insertions(+), 27 deletions(-) create mode 100644 projects/trivia/src/tests/invite-mail-friends.component.ts create mode 100644 projects/trivia/src/tests/setup.ts diff --git a/karma.conf.js b/karma.conf.js index 146540717..0aed46861 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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: [ diff --git a/projects/trivia/src/tests/check-display-name.component.ts b/projects/trivia/src/tests/check-display-name.component.ts index ba2f8d5a5..b05120511 100644 --- a/projects/trivia/src/tests/check-display-name.component.ts +++ b/projects/trivia/src/tests/check-display-name.component.ts @@ -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; + let spy: any; + const event = 'new text change'; + afterAll(() => { }); beforeEach(nsTestBedBeforeEach([CheckDisplayNameComponent], [ { @@ -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); }); }); diff --git a/projects/trivia/src/tests/example.ts b/projects/trivia/src/tests/example.ts index 8bdb09a84..95f73e1a1 100644 --- a/projects/trivia/src/tests/example.ts +++ b/projects/trivia/src/tests/example.ts @@ -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); }); diff --git a/projects/trivia/src/tests/invite-mail-friends.component.ts b/projects/trivia/src/tests/invite-mail-friends.component.ts new file mode 100644 index 000000000..07c946cab --- /dev/null +++ b/projects/trivia/src/tests/invite-mail-friends.component.ts @@ -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; + let mockStore: MockStore; + let spy: any; + let mockCoreSelector: MemoizedSelector>; + 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, {}); + 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('test@test.com'); + 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('test@test.com'); + expect(component.invitationForm.valid).toBeTruthy(); + }); + + + it('Check isValid will return true if email is valid', () => { + expect(component.isValid('test@test.com')).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,test@mail.com'); + 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,test@mail.com,trivia@mail.com'); + 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('trivia@mail.com'); + const user = { ...testData.userList[0] }; + mockCoreSelector.setResult({ user }); + mockStore.refreshState(); + + const payloadData = { + userId: user.userId, + emails: ['trivia@mail.com'] + }; + + spy.and.callFake((action: ActionWithPayload) => { + 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 user@user.com'; + 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(); + }); +}); diff --git a/projects/trivia/src/tests/setup.ts b/projects/trivia/src/tests/setup.ts new file mode 100644 index 000000000..082c4e250 --- /dev/null +++ b/projects/trivia/src/tests/setup.ts @@ -0,0 +1,3 @@ +import 'nativescript-angular/zone-js/testing.jasmine'; +import {nsTestBedInit} from 'nativescript-angular/testing'; +nsTestBedInit(); diff --git a/tsconfig.tns.json b/tsconfig.tns.json index 922edba24..b27f7c774 100644 --- a/tsconfig.tns.json +++ b/tsconfig.tns.json @@ -10,6 +10,7 @@ "shared-library/*": [ "./projects/shared-library/src/lib/*" ], + "test/*": ["./test/*"], "*": [ "./node_modules/tns-core-modules/*", "./node_modules/*"