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

test: edit register.effects.spec to reduce describe depth #3369

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 100 additions & 104 deletions libs/auth/state/src/effects/register.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action } from '@ngrx/store';
import {
hot,
cold,
Expand All @@ -8,6 +9,7 @@ import {
Observable,
of,
} from 'rxjs';
import { TestScheduler } from 'rxjs/testing';

import {
DaffLoginInfo,
Expand Down Expand Up @@ -39,6 +41,15 @@ import { daffTransformErrorToStateError } from '@daffodil/core/state';

import { DaffAuthRegisterEffects } from './register.effects';

interface RegisterTestStates {
isAutoLoginTrue: boolean;
didRegisterSucceed: boolean;
didTokenStorageSucceed?: boolean;
whatErrorWasThrown?: DaffStorageServiceError;
whatActionWasReturned?: Action;
whatOtherActionWasReturned?: Action;
}

describe('@daffodil/auth/state | DaffAuthRegisterEffects', () => {
let actions$: Observable<any>;
let effects: DaffAuthRegisterEffects;
Expand Down Expand Up @@ -90,115 +101,100 @@ describe('@daffodil/auth/state | DaffAuthRegisterEffects', () => {
});

describe('register$ | when the user registers an account', () => {
let expected;
let mockAuthRegisterAction: DaffAuthRegister;

describe('when autoLogin is true', () => {
beforeEach(() => {
mockAuthRegisterAction = new DaffAuthRegister(mockRegistration, true);
actions$ = hot('--a', { a: mockAuthRegisterAction });
});
it('should compute the next action correctly', () => {
const testStates: RegisterTestStates[] = [
{
isAutoLoginTrue: true,
didRegisterSucceed: true,
didTokenStorageSucceed: true,
whatActionWasReturned: new DaffAuthRegisterSuccess(token),
},
{
isAutoLoginTrue: true,
didRegisterSucceed: true,
didTokenStorageSucceed: false,
whatErrorWasThrown: new DaffServerSideStorageError('Server side'),
whatActionWasReturned: new DaffAuthRegisterFailure(daffTransformErrorToStateError(
new DaffServerSideStorageError('Server side'),
)),
whatOtherActionWasReturned: new DaffAuthServerSide(daffTransformErrorToStateError(
new DaffServerSideStorageError('Server side'),
)),
},
{
isAutoLoginTrue: true,
didRegisterSucceed: true,
didTokenStorageSucceed: false,
whatErrorWasThrown: new DaffStorageServiceError('Storage error'),
whatActionWasReturned: new DaffAuthRegisterFailure(daffTransformErrorToStateError(
new DaffStorageServiceError('Storage error'),
)),
whatOtherActionWasReturned: new DaffAuthStorageFailure(daffTransformErrorToStateError(
new DaffStorageServiceError('Storage error'),
)),
},
{
isAutoLoginTrue: true,
didRegisterSucceed: false,
whatErrorWasThrown: new DaffAuthInvalidAPIResponseError('Failed to register a new user'),
whatActionWasReturned: new DaffAuthRegisterFailure(daffTransformErrorToStateError(
new DaffAuthInvalidAPIResponseError('Failed to register a new user'),
)),
},
{
isAutoLoginTrue: false,
didRegisterSucceed: true,
whatActionWasReturned: new DaffAuthRegisterSuccess(),
},
{
isAutoLoginTrue: false,
didRegisterSucceed: false,
whatErrorWasThrown: new DaffAuthInvalidAPIResponseError('Failed to register a new user'),
whatActionWasReturned: new DaffAuthRegisterFailure(daffTransformErrorToStateError(
new DaffAuthInvalidAPIResponseError('Failed to register a new user'),
)),
},
];

describe('and the register is successful', () => {
beforeEach(() => {
daffRegisterDriver.register.and.returnValue(of(token));
testStates.forEach((el) => {
const testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});

describe('and setToken is successful', () => {
beforeEach(() => {
const mockAuthResetPasswordSuccessAction = new DaffAuthRegisterSuccess(token);

expected = cold('--a', { a: mockAuthResetPasswordSuccessAction });
});

it('should notify state that the register was successful', () => {
expect(effects.register$).toBeObservable(expected);
});

it('should store the auth token', () => {
expect(effects.register$).toBeObservable(expected);
testScheduler.run(helpers => {
const mockAuthRegisterAction = new DaffAuthRegister(mockRegistration, el.isAutoLoginTrue);
actions$ = helpers.hot('--a', { a: mockAuthRegisterAction });

if (el.didRegisterSucceed) {
if (el.isAutoLoginTrue) {
daffRegisterDriver.register.and.returnValue(of(token));
} else {
daffRegisterDriver.registerOnly.and.returnValue(of(undefined));
}
if (el.whatErrorWasThrown) {
setAuthTokenSpy.and.throwError(el.whatErrorWasThrown);
} else {
setAuthTokenSpy.and.returnValue(undefined);
}
} else {
if (el.isAutoLoginTrue) {
daffRegisterDriver.register.and.returnValue(helpers.cold('#', {}, el.whatErrorWasThrown));
} else {
daffRegisterDriver.registerOnly.and.returnValue(helpers.cold('#', {}, el.whatErrorWasThrown));
}
}

if (el.didRegisterSucceed && el.whatErrorWasThrown){
helpers.expectObservable(effects.register$).toBe('--(ab)', { a: el.whatOtherActionWasReturned, b: el.whatActionWasReturned });
} else {
helpers.expectObservable(effects.register$).toBe('--b', { b: el.whatActionWasReturned });
}

helpers.flush();
if (el.didTokenStorageSucceed) {
expect(setAuthTokenSpy).toHaveBeenCalledWith(token);
});
});

describe('and the storage service throws a server side error', () => {
beforeEach(() => {
const error = new DaffServerSideStorageError('Server side');
const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));
setAuthTokenSpy.and.throwError(error);
expected = cold('--(ab)', { a: serverSideAction, b: mockAuthResetPasswordFailureAction });
});

it('should dispatch a server side and a failure action', () => {
expect(effects.register$).toBeObservable(expected);
});
});

describe('and the storage service throws a storage error', () => {
beforeEach(() => {
const error = new DaffStorageServiceError('Storage error');
const storageAction = new DaffAuthStorageFailure(daffTransformErrorToStateError(error));
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));
setAuthTokenSpy.and.throwError(error);
expected = cold('--(ab)', { a: storageAction, b: mockAuthResetPasswordFailureAction });
});

it('should dispatch a server side action', () => {
expect(effects.register$).toBeObservable(expected);
});
});
});

describe('and the register fails', () => {
beforeEach(() => {
const error = new DaffAuthInvalidAPIResponseError('Failed to register a new user');
const response = cold('#', {}, error);
daffRegisterDriver.register.and.returnValue(response);
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));

actions$ = hot('--a', { a: mockAuthRegisterAction });
expected = cold('--b', { b: mockAuthResetPasswordFailureAction });
});

it('should notify state that the register failed', () => {
expect(effects.register$).toBeObservable(expected);
});
});
});

describe('when autoLogin is false', () => {
beforeEach(() => {
mockAuthRegisterAction = new DaffAuthRegister(mockRegistration, false);
});

describe('and the register is successful', () => {
beforeEach(() => {
daffRegisterDriver.registerOnly.and.returnValue(of(undefined));
const mockAuthResetPasswordSuccessAction = new DaffAuthRegisterSuccess();

actions$ = hot('--a', { a: mockAuthRegisterAction });
expected = cold('--b', { b: mockAuthResetPasswordSuccessAction });
});

it('should notify state that the register was successful', () => {
expect(effects.register$).toBeObservable(expected);
});
});

describe('and the register fails', () => {
beforeEach(() => {
const error = new DaffAuthInvalidAPIResponseError('Failed to register a new user');
const response = cold('#', {}, error);
daffRegisterDriver.registerOnly.and.returnValue(response);
const mockAuthResetPasswordFailureAction = new DaffAuthRegisterFailure(daffTransformErrorToStateError(error));

actions$ = hot('--a', { a: mockAuthRegisterAction });
expected = cold('--b', { b: mockAuthResetPasswordFailureAction });
});

it('should notify state that the register failed', () => {
expect(effects.register$).toBeObservable(expected);
}
});
});
});
Expand Down
Loading