From d7ab5a4c1d19aef3b9f87017782bf99588d6ab84 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 15 Oct 2019 14:26:36 +0300 Subject: [PATCH] test(core): add profile.state.spec --- .../core/src/lib/tests/profile.state.spec.ts | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/profile.state.spec.ts diff --git a/npm/ng-packs/packages/core/src/lib/tests/profile.state.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/profile.state.spec.ts new file mode 100644 index 0000000000..1ebdda217a --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/profile.state.spec.ts @@ -0,0 +1,74 @@ +import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; +import { Session } from '../models/session'; +import { ProfileService } from '../services'; +import { ProfileState } from '../states'; +import { GetAppConfiguration } from '../actions/config.actions'; +import { of } from 'rxjs'; +import { Profile } from '../models/profile'; + +export class DummyClass {} + +export const PROFILE_STATE_DATA = { + profile: { userName: 'admin', email: 'info@abp.io', name: 'Admin' }, +} as Profile.State; + +describe('ProfileState', () => { + let spectator: SpectatorService; + let state: ProfileState; + let profileService: SpyObject; + let patchedData; + const patchState = jest.fn(data => (patchedData = data)); + + const createService = createServiceFactory({ + service: DummyClass, + mocks: [ProfileService], + }); + + beforeEach(() => { + spectator = createService(); + profileService = spectator.get(ProfileService); + state = new ProfileState(profileService); + }); + + describe('#getProfile', () => { + it('should return the current language', () => { + expect(ProfileState.getProfile(PROFILE_STATE_DATA)).toEqual(PROFILE_STATE_DATA.profile); + }); + }); + + describe('#GetProfile', () => { + it('should call the profile service get method and update the state', () => { + const mockData = { userName: 'test', email: 'test@abp.io' }; + const spy = jest.spyOn(profileService, 'get'); + spy.mockReturnValue(of(mockData as any)); + + state.profileGet({ patchState } as any).subscribe(); + + expect(patchedData).toEqual({ profile: mockData }); + }); + }); + + describe('#UpdateProfile', () => { + it('should call the profile service update method and update the state', () => { + const mockData = { userName: 'test2', email: 'test@abp.io' }; + const spy = jest.spyOn(profileService, 'update'); + spy.mockReturnValue(of(mockData as any)); + + state.profileUpdate({ patchState } as any, { payload: mockData as any }).subscribe(); + + expect(patchedData).toEqual({ profile: mockData }); + }); + }); + + describe('#ChangePassword', () => { + it('should call the profile service changePassword method', () => { + const mockData = { currentPassword: 'test123', newPassword: 'test123' }; + const spy = jest.spyOn(profileService, 'changePassword'); + spy.mockReturnValue(of(null)); + + state.changePassword(null, { payload: mockData }).subscribe(); + + expect(spy).toHaveBeenCalledWith(mockData, true); + }); + }); +});