Skip to content

Commit 66080cf

Browse files
committed
test(palette-service): correctly mock palette service for tests
1 parent 5530819 commit 66080cf

File tree

9 files changed

+60
-36
lines changed

9 files changed

+60
-36
lines changed

src/app/home/home.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { Router } from '@angular/router';
33
import { TranslateModule } from '@ngx-translate/core';
44
import { AnalyticsService, AnalyticsServiceMock } from '../shared/data-access/analytics.service';
5-
import { PaletteService, PaletteServiceMock } from '../shared/data-access/palette.service';
5+
import { PaletteService } from '../shared/data-access/palette.service';
6+
import { PaletteServiceMock } from '../shared/data-access/palette.service-mock';
67
import { HomeService, HomeServiceMock } from './data-access/home.service';
78
import HomeComponent from './home.component';
89

src/app/list/list.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { ActivatedRoute } from '@angular/router';
33
import { TranslateModule } from '@ngx-translate/core';
44
import { DialogService, DialogServiceMock } from '../shared/data-access/dialog.service';
55
import { ListService, ListServiceMock } from '../shared/data-access/list.service';
6-
import { PaletteService, PaletteServiceMock } from '../shared/data-access/palette.service';
6+
import { PaletteService } from '../shared/data-access/palette.service';
7+
import { PaletteServiceMock } from '../shared/data-access/palette.service-mock';
78
import ListComponent from './list.component';
89

910
describe('ListComponent', () => {

src/app/shared/data-access/confetti.service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TestBed } from '@angular/core/testing';
22
import { ConfettiService } from './confetti.service';
3-
import { PaletteService, PaletteServiceMock } from './palette.service';
3+
import { PaletteService } from './palette.service';
4+
import { PaletteServiceMock } from './palette.service-mock';
45

56
describe('ConfettiService', () => {
67
let service: ConfettiService;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { signal } from '@angular/core';
2+
import { PaletteScheme } from '../constants/palette-scheme';
3+
import { Tailwind, TailwindGrays, TailwindRainbow } from '../constants/tailwind-colors';
4+
import { Color } from '../model/color.model';
5+
import { Palette } from '../model/palette.model';
6+
import { Shade } from '../model/shade.model';
7+
8+
/**
9+
* The original service is located at ./palette.service.ts
10+
* This mock in in this separate file to not include the Tailwind example palettes in the production bundle.
11+
*/
12+
13+
export class PaletteServiceMock {
14+
public readonly palette = signal<Palette | undefined>(
15+
new Palette('Mock', [new Color([Shade.random()], 'MockColor')], 'test-id')
16+
);
17+
public readonly isGenerating = signal(false);
18+
public loadPaletteFromLocalStorage(id: string, _onlyReturn: boolean): Palette | undefined {
19+
switch (id) {
20+
case 'test-id':
21+
return this.palette();
22+
case TailwindRainbow.id:
23+
return TailwindRainbow;
24+
case TailwindGrays.id:
25+
return TailwindGrays;
26+
case Tailwind.id:
27+
return Tailwind;
28+
default:
29+
return undefined;
30+
}
31+
}
32+
public savePaletteToLocalStorage(_upgrade: boolean): void {}
33+
public duplicatePalette(_name?: string): string {
34+
return 'test-id';
35+
}
36+
public async generatePalette(_hex: string, _scheme: PaletteScheme): Promise<string> {
37+
return 'test-id';
38+
}
39+
}

src/app/shared/data-access/palette.service.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,8 @@ export class PaletteService {
564564
}
565565
}
566566

567-
export class PaletteServiceMock {
568-
public palette = signal<Palette | undefined>(
569-
new Palette('Mock', [new Color([Shade.random()], 'MockColor')], 'test-id')
570-
);
571-
public isGenerating = signal(false);
572-
public loadPaletteFromLocalStorage(): void {}
573-
public savePaletteToLocalStorage(): void {}
574-
public generatePalette(_hex: string, _scheme: PaletteScheme): string {
575-
return 'test-id';
576-
}
577-
}
567+
/**
568+
* The corresponding mock for this service is located in ./palette.service-mock.ts
569+
* This is because the mock uses the Tailwind example palettes under the hood.
570+
* We don't want to include these in the production bundle, but only in the testing environment keeping the bundle size smaller.
571+
*/

src/app/shared/data-access/pwa.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { SwUpdateMock } from '../utils/sw-update-mock';
77
import { AnalyticsService, AnalyticsServiceMock } from './analytics.service';
88
import { ConfettiService, ConfettiServiceMock } from './confetti.service';
99
import { DialogService, DialogServiceMock } from './dialog.service';
10-
import { PaletteService, PaletteServiceMock } from './palette.service';
10+
import { PaletteService } from './palette.service';
11+
import { PaletteServiceMock } from './palette.service-mock';
1112
import { PwaService } from './pwa.service';
1213
import { ToastService, ToastServiceMock } from './toast.service';
1314
import { VersionService, VersionServiceMock } from './version.service';
@@ -106,7 +107,6 @@ describe('PwaService', () => {
106107

107108
expect(dialogService.confirm).toHaveBeenCalledTimes(1);
108109
expect(paletteService.savePaletteToLocalStorage).toHaveBeenCalledTimes(1);
109-
// @ts-expect-error Function has an optional parameter which does not get detected here
110110
expect(paletteService.savePaletteToLocalStorage).toHaveBeenCalledWith(true);
111111
expect(analyticsService.trackEvent).toHaveBeenCalledWith(
112112
TrackingEventCategory.PWA,

src/app/view/ui/import-color/import-color.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { DIALOG_DATA, DialogRef } from '@angular/cdk/dialog';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { TranslateModule } from '@ngx-translate/core';
44
import { ListService, ListServiceMock } from '../../../shared/data-access/list.service';
5-
import { PaletteService, PaletteServiceMock } from '../../../shared/data-access/palette.service';
5+
import { PaletteService } from '../../../shared/data-access/palette.service';
6+
import { PaletteServiceMock } from '../../../shared/data-access/palette.service-mock';
67
import { ImportColorComponent } from './import-color.component';
78

89
describe('ImportColorComponent', () => {

src/app/view/ui/import-color/import-color.component.stories.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,17 @@ import { DIALOG_DATA, DialogRef } from '@angular/cdk/dialog';
22
import { Meta, applicationConfig } from '@storybook/angular';
33
import { Tailwind, TailwindGrays, TailwindRainbow } from '../../../shared/constants/tailwind-colors';
44
import { ListService, ListServiceMock } from '../../../shared/data-access/list.service';
5-
import { PaletteService, PaletteServiceMock } from '../../../shared/data-access/palette.service';
6-
import { Palette } from '../../../shared/model';
5+
import { PaletteService } from '../../../shared/data-access/palette.service';
6+
import { PaletteServiceMock } from '../../../shared/data-access/palette.service-mock';
77
import { createStory } from '../../../shared/utils/storybook';
88
import { ImportColorComponent } from './import-color.component';
99

1010
const listService = new ListServiceMock();
11+
listService.remove('test-id');
1112
listService.add(TailwindRainbow);
1213
listService.add(TailwindGrays);
1314
listService.add(Tailwind);
1415

15-
const paletteService = new PaletteServiceMock();
16-
// @ts-expect-error - Types of this mock are not correct
17-
paletteService.loadPaletteFromLocalStorage = (id: string): Palette | undefined => {
18-
switch (id) {
19-
case TailwindRainbow.id:
20-
return TailwindRainbow;
21-
case TailwindGrays.id:
22-
return TailwindGrays;
23-
case Tailwind.id:
24-
return Tailwind;
25-
default:
26-
return undefined;
27-
}
28-
};
29-
3016
const meta: Meta<ImportColorComponent> = {
3117
title: 'View/Import Color',
3218
component: ImportColorComponent,
@@ -48,7 +34,7 @@ const meta: Meta<ImportColorComponent> = {
4834
},
4935
{
5036
provide: PaletteService,
51-
useValue: paletteService
37+
useClass: PaletteServiceMock
5238
}
5339
]
5440
})

src/app/view/view.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { AnalyticsService, AnalyticsServiceMock } from '../shared/data-access/an
1010
import { ColorService, ColorServiceMock } from '../shared/data-access/color.service';
1111
import { DialogService, DialogServiceMock } from '../shared/data-access/dialog.service';
1212
import { ListService, ListServiceMock } from '../shared/data-access/list.service';
13-
import { PaletteService, PaletteServiceMock } from '../shared/data-access/palette.service';
13+
import { PaletteService } from '../shared/data-access/palette.service';
14+
import { PaletteServiceMock } from '../shared/data-access/palette.service-mock';
1415
import { PwaService, PwaServiceMock } from '../shared/data-access/pwa.service';
1516
import { ToastService, ToastServiceMock } from '../shared/data-access/toast.service';
1617
import { TrackingEventAction, TrackingEventCategory } from '../shared/enums/tracking-event';

0 commit comments

Comments
 (0)