mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
86016: Add unit tests for correlationId reducer & service
This commit is contained in:
23
src/app/correlation-id/correlation-id.reducer.spec.ts
Normal file
23
src/app/correlation-id/correlation-id.reducer.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { correlationIdReducer } from './correlation-id.reducer';
|
||||||
|
import { SetCorrelationIdAction } from './correlation-id.actions';
|
||||||
|
|
||||||
|
describe('correlationIdReducer', () => {
|
||||||
|
it('should set the correlatinId with SET action', () => {
|
||||||
|
const initialState = null;
|
||||||
|
const currentState = correlationIdReducer(initialState, new SetCorrelationIdAction('new ID'));
|
||||||
|
|
||||||
|
expect(currentState).toBe('new ID');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should leave correlatinId unchanged otherwise', () => {
|
||||||
|
const initialState = null;
|
||||||
|
|
||||||
|
let currentState = correlationIdReducer(initialState, { type: 'unknown' } as any);
|
||||||
|
expect(currentState).toBe(null);
|
||||||
|
|
||||||
|
currentState = correlationIdReducer(currentState, new SetCorrelationIdAction('new ID'));
|
||||||
|
currentState = correlationIdReducer(currentState, { type: 'unknown' } as any);
|
||||||
|
|
||||||
|
expect(currentState).toBe('new ID');
|
||||||
|
});
|
||||||
|
});
|
83
src/app/correlation-id/correlation-id.service.spec.ts
Normal file
83
src/app/correlation-id/correlation-id.service.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import { CorrelationIdService } from './correlation-id.service';
|
||||||
|
import { CookieServiceMock } from '../shared/mocks/cookie.service.mock';
|
||||||
|
import { UUIDService } from '../core/shared/uuid.service';
|
||||||
|
import { MockStore, provideMockStore } from '@ngrx/store/testing';
|
||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { Store, StoreModule } from '@ngrx/store';
|
||||||
|
import { appReducers, AppState, storeModuleConfig } from '../app.reducer';
|
||||||
|
import { SetCorrelationIdAction } from './correlation-id.actions';
|
||||||
|
|
||||||
|
fdescribe('CorrelationIdService', () => {
|
||||||
|
let service: CorrelationIdService;
|
||||||
|
|
||||||
|
let cookieService;
|
||||||
|
let uuidService;
|
||||||
|
let store;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await TestBed.configureTestingModule({
|
||||||
|
imports: [
|
||||||
|
StoreModule.forRoot(appReducers, storeModuleConfig),
|
||||||
|
],
|
||||||
|
}).compileComponents();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cookieService = new CookieServiceMock();
|
||||||
|
uuidService = new UUIDService();
|
||||||
|
store = TestBed.inject(Store) as MockStore<AppState>;
|
||||||
|
service = new CorrelationIdService(cookieService, uuidService, store);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getCorrelationId', () => {
|
||||||
|
it('should get from from store', () => {
|
||||||
|
expect(service.getCorrelationId()).toBe(null);
|
||||||
|
store.dispatch(new SetCorrelationIdAction('some value'));
|
||||||
|
expect(service.getCorrelationId()).toBe('some value');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('initCorrelationId', () => {
|
||||||
|
const cookieCID = 'cookie CID';
|
||||||
|
const storeCID = 'store CID';
|
||||||
|
|
||||||
|
it('should set cookie and store values to a newly generated value if neither ex', () => {
|
||||||
|
service.initCorrelationId();
|
||||||
|
|
||||||
|
expect(cookieService.get('CORRELATION-ID')).toBeTruthy();
|
||||||
|
expect(service.getCorrelationId()).toBeTruthy();
|
||||||
|
expect(cookieService.get('CORRELATION-ID')).toEqual(service.getCorrelationId());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set store value to cookie value if present', () => {
|
||||||
|
expect(service.getCorrelationId()).toBe(null);
|
||||||
|
|
||||||
|
cookieService.set('CORRELATION-ID', cookieCID);
|
||||||
|
|
||||||
|
service.initCorrelationId();
|
||||||
|
|
||||||
|
expect(cookieService.get('CORRELATION-ID')).toBe(cookieCID);
|
||||||
|
expect(service.getCorrelationId()).toBe(cookieCID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set cookie value to store value if present', () => {
|
||||||
|
store.dispatch(new SetCorrelationIdAction(storeCID));
|
||||||
|
|
||||||
|
service.initCorrelationId();
|
||||||
|
|
||||||
|
expect(cookieService.get('CORRELATION-ID')).toBe(storeCID);
|
||||||
|
expect(service.getCorrelationId()).toBe(storeCID);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set store value to cookie value if both are present', () => {
|
||||||
|
cookieService.set('CORRELATION-ID', cookieCID);
|
||||||
|
store.dispatch(new SetCorrelationIdAction(storeCID));
|
||||||
|
|
||||||
|
service.initCorrelationId();
|
||||||
|
|
||||||
|
expect(cookieService.get('CORRELATION-ID')).toBe(cookieCID);
|
||||||
|
expect(service.getCorrelationId()).toBe(cookieCID);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in New Issue
Block a user