Skip to content

Commit

Permalink
fix: testing FavoritesService
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Apr 22, 2024
1 parent f3ed8ec commit 1b5f94b
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 36 deletions.
23 changes: 11 additions & 12 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,25 @@
"karmaTestExplorer.projectWorkspaces": [
{
"rootPath": "projects/aas-lib",
"karmaConfFilePath": "karma.config.cjs",
// "karmaConfFilePath": "karma.config.cjs",
"testFramework": "jasmine",
"projectType": "angular",
"testsBasePath": "src/test",
"testFiles": [
"src/**/*.spec.ts"
],
"excludeFiles": ["dist/**/*.*"]
// "testFiles": [
// "src/**/*.spec.ts"
// ],
// "excludeFiles": ["dist/**/*.*"]
},
{
"rootPath": "projects/aas-portal",
"karmaConfFilePath": "karma.config.cjs",
"testFramework": "jasmine",
"projectType": "angular",
"testsBasePath": "src/test",
"testFiles": [
"src/**/*.spec.ts"
],
"excludeFiles": ["dist/**/*.*"]
// "testsBasePath": "src/test",
// "testFiles": [
// "src/**/*.spec.ts"
// ],
// "excludeFiles": ["dist/**/*.*"]
}
],

]
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions projects/aas-portal/src/app/start/favorites.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,9 @@ export class FavoritesService {
return this.lists$.getValue().find(list => list.name === name);
}

public delete(name: string): Observable<void> {
return this.lists$.pipe(
first(),
map(lists => lists.filter(list => list.name !== name)),
map(lists => this.lists$.next(lists)),
mergeMap(lists => this.auth.setCookie('.Favorites', JSON.stringify(lists))),
);
}

public add(documents: AASDocument[], name: string, newName?: string): Observable<void> {
return this.lists$.pipe(
first(),
map(lists => [...lists]),
map(lists => {
const i = lists.findIndex(list => list.name === name);
Expand All @@ -71,29 +63,49 @@ export class FavoritesService {

return lists;
}),
map(lists => this.lists$.next(lists)),
mergeMap(lists => this.auth.setCookie('.Favorites', JSON.stringify(lists))),
mergeMap(lists => {
this.lists$.next(lists);
return this.auth.setCookie('.Favorites', JSON.stringify(lists));
}),
);
}

public remove(documents: AASDocument[], name: string): Observable<void> {
return this.lists$.pipe(
first(),
map(lists => [...lists]),
map(lists => {
const i = lists.findIndex(list => list.name === name);
if (i < 0) {
throw new Error(`A favorites list with the name "${name}" does not exists.`);
throw new Error(`A favorites list "${name}" does not exists.`);
}

const list = { ...lists[i] };
list.documents = list.documents.filter(favorite =>
documents.every(document => favorite.endpoint !== document.endpoint || favorite.id !== document.id),
);

lists[i] = list;

return lists;
}),
map(lists => this.lists$.next(lists)),
mergeMap(list => this.auth.setCookie('.Favorites', JSON.stringify(list))),
mergeMap(lists => {
this.lists$.next(lists);
return this.auth.setCookie('.Favorites', JSON.stringify(lists));
}),
);
}

public delete(name: string): Observable<void> {
return this.lists$.pipe(
first(),
map(lists => lists.filter(list => list.name !== name)),
mergeMap(lists => {
this.lists$.next(lists);
return lists.length > 0
? this.auth.setCookie('.Favorites', JSON.stringify(lists))
: this.auth.deleteCookie('.Favorites');
}),
);
}
}
106 changes: 103 additions & 3 deletions projects/aas-portal/src/test/start/favorites.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@
*****************************************************************************/

import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { first, of } from 'rxjs';
import { AuthService } from 'aas-lib';
import { TranslateFakeLoader, TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { FavoritesService } from '../../app/start/favorites.service';
import { FavoritesList } from '../../app/start/start.state';
import { AASDocument } from 'projects/common/dist/types';

describe('FavoritesService', () => {
let service: FavoritesService;
let auth: jasmine.SpyObj<AuthService>;
const favorite: AASDocument = {
address: 'http://localhost/aas',
crc32: 0,
idShort: 'AAS',
readonly: false,
timestamp: 0,
id: 'http://localhost/aas',
endpoint: 'endpoint',
};

const favorites: FavoritesList[] = [
{
name: 'My Favorites',
documents: [favorite],
},
];

beforeEach(() => {
auth = jasmine.createSpyObj<AuthService>(['getCookie', 'setCookie']);
auth.getCookie.and.returnValue(of(undefined));
auth = jasmine.createSpyObj<AuthService>(['getCookie', 'setCookie', 'deleteCookie']);
auth.getCookie.and.returnValue(of(JSON.stringify(favorites)));
auth.setCookie.and.returnValue(of(void 0));
auth.deleteCookie.and.returnValue(of(void 0));

TestBed.configureTestingModule({
providers: [
Expand All @@ -37,10 +56,91 @@ describe('FavoritesService', () => {
}),
],
});

service = TestBed.inject(FavoritesService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

describe('lists', () => {
it('provides all favorites lists', (done: DoneFn) => {
service.lists.pipe(first()).subscribe(values => {
expect(values).toEqual(favorites);
done();
});
});
});

describe('has', () => {
it('has "My Favorites"', () => {
expect(service.has('My Favorites')).toBeTrue();
});

it('has not "Unknown"', () => {
expect(service.has('Unknown')).toBeFalse();
});
});

describe('get', () => {
it('gets the favorites list "My Favorites"', () => {
expect(service.get('My Favorites')).toEqual(favorites[0]);
});

it('gets `undefined` for "Unknown"', () => {
expect(service.get('Unknown')).toBeUndefined();
});
});

describe('add', () => {
it('adds a new favorites list', (done: DoneFn) => {
service.add([], 'New Favorites').subscribe(() => {
expect(auth.setCookie).toHaveBeenCalledWith(
'.Favorites',
JSON.stringify([{ ...favorites[0] }, { name: 'New Favorites', documents: [] }] as FavoritesList[]),
);

done();
});
});

it('renames a favorites list', (done: DoneFn) => {
service.add([], 'My Favorites', 'New Favorites').subscribe(() => {
expect(auth.setCookie).toHaveBeenCalledWith(
'.Favorites',
JSON.stringify([{ name: 'New Favorites', documents: favorites[0].documents }] as FavoritesList[]),
);

done();
});
});
});

describe('delete', () => {
it('deletes "My Favorites"', (done: DoneFn) => {
service.delete('My Favorites').subscribe(() => {
expect(auth.deleteCookie).toHaveBeenCalledWith('.Favorites');
done();
});
});
});

describe('remove', () => {
it('removes a favorite', (done: DoneFn) => {
service.remove([favorite], 'My Favorites').subscribe(() => {
expect(auth.setCookie).toHaveBeenCalledWith(
'.Favorites',
JSON.stringify([
{
name: 'My Favorites',
documents: [],
},
]),
);

done();
});
});
});
});
4 changes: 2 additions & 2 deletions projects/fhg-jest/esbuild.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as esbuild from 'esbuild';

await esbuild.build({
entryPoints: ['./src/lib/index.ts'],
outfile: './dist/awp-jest.js',
outfile: './dist/fhg-jest.js',
bundle: true,
platform: 'neutral',
format: 'esm',
tsconfig: 'tsconfig.lib.json',
external: ['lodash-es', 'reflect-metadata', '@jest/globals'],
});
});
4 changes: 2 additions & 2 deletions projects/fhg-jest/esbuild.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as esbuild from 'esbuild';

await esbuild.build({
entryPoints: ['./src/lib/index.ts'],
outfile: './dist/awp-jest.js',
outfile: './dist/fhg-jest.js',
bundle: true,
platform: 'neutral',
format: 'esm',
tsconfig: 'tsconfig.lib.json',
minify: true,
external: ['lodash-es', 'reflect-metadata', '@jest/globals'],
});
});
2 changes: 1 addition & 1 deletion projects/fhg-jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['<rootDir>/src/lib/**/*.ts'],
coverageDirectory: '<rootDir>/../../reports/awp-jest',
coverageDirectory: '<rootDir>/../../reports/fhg-jest',
coverageReporters: ['html', 'json-summary', 'cobertura'],
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
Expand Down

0 comments on commit 1b5f94b

Please sign in to comment.