| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { PageRequestInterface } from '@shared/models/page/page-request.interface'; |
| import { take } from 'rxjs/operators'; |
| import { ContactsEffects } from '@shared/store/effects/contacts.effect'; |
| import { Subject, of, throwError } from 'rxjs'; |
| import { Contact } from '@app/shared/models'; |
| import * as contactsActions from '@shared/store/actions/contacts.action'; |
| import { ContactsApiClient } from '@app/pages/contacts/contacts-api-client'; |
| import { PageModel } from '@app/shared/models/page/page.model'; |
| |
| |
| describe('Contacts Effects', () => { |
| |
| let effects: ContactsEffects; |
| let actions$: Subject<any>; |
| let apiClient: ContactsApiClient; |
| let apiResponse = new PageModel<Contact>( |
| { |
| content: [new Contact({id: "1"})] |
| } |
| ) as any; |
| |
| beforeEach(() => { |
| apiClient = { |
| getContacts() {} |
| } as any; |
| actions$ = new Subject(); |
| effects = new ContactsEffects(actions$, apiClient); |
| }); |
| |
| it('should be truthy', () => { |
| expect(effects).toBeTruthy(); |
| }); |
| |
| it('should equal loadContactsSuccess after getContacts', (done) => { |
| spyOn(apiClient, 'getContacts').and.returnValue(of(apiResponse)); |
| effects.getContacts$.pipe(take(1)).subscribe(result => { |
| expect(result).toEqual(contactsActions.loadContactsSuccess({payload: apiResponse.content})); |
| }); |
| done(); |
| actions$.next(contactsActions.loadContacts()); |
| }); |
| |
| it('should equal loadContactsFail after getContacts Error', (done) => { |
| spyOn(apiClient, 'getContacts').and.returnValue(throwError('x')); |
| |
| effects.getContacts$.pipe(take(1)).subscribe(result => { |
| expect(result).toEqual(contactsActions.loadContactsFail({payload: 'x'})); |
| }); |
| done(); |
| |
| actions$.next(contactsActions.loadContacts()); |
| }); |
| |
| it('should equal loadContactsPageSuccess after getContactsPage', (done) => { |
| let request = {payload: { |
| pageNumber: 1, |
| pageSize: 3, |
| } as PageRequestInterface |
| } |
| spyOn(apiClient, 'getContacts').and.returnValue(of(apiResponse)); |
| |
| effects.getContactsPage$.pipe(take(1)).subscribe(result => { |
| expect(result).toEqual(contactsActions.loadContactsPageSuccess({payload: apiResponse})); |
| }); |
| done(); |
| |
| actions$.next(contactsActions.loadContactsPage(request)); |
| }); |
| |
| it('should equal loadContactsPageFail after getContactsPage Error', (done) => { |
| let request = {payload: { |
| pageNumber: 1, |
| pageSize: 3, |
| } as PageRequestInterface |
| } |
| spyOn(apiClient, 'getContacts').and.returnValue(throwError('x')); |
| |
| effects.getContactsPage$.pipe(take(1)).subscribe(result => { |
| expect(result).toEqual(contactsActions.loadContactsPageFail({payload: 'x'})); |
| }); |
| done(); |
| |
| actions$.next(contactsActions.loadContactsPage(request)); |
| }); |
| |
| }); |