Skip to content

Commit

Permalink
test: extend skin service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmcquade committed Sep 18, 2024
1 parent d3ac324 commit 27712b0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
32 changes: 31 additions & 1 deletion src/app/feature/theme/services/theme.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import { TestBed } from "@angular/core/testing";

import { ThemeService } from "./theme.service";
import { LocalStorageService } from "src/app/shared/services/local-storage/local-storage.service";
import { MockLocalStorageService } from "src/app/shared/services/local-storage/local-storage.service.spec";
import { AppConfigService } from "src/app/shared/services/app-config/app-config.service";
import { MockAppConfigService } from "src/app/shared/services/app-config/app-config.service.spec";
import { IAppConfig } from "packages/data-models";

export class MockThemeService implements Partial<ThemeService> {
ready() {
return true;
}
setTheme() {}
getCurrentTheme() {
return "mock_theme";
}
}

const MOCK_APP_CONFIG: Partial<IAppConfig> = {
APP_THEMES: {
available: ["MOCK_THEME_1", "MOCK_THEME_2"],
defaultThemeName: "MOCK_THEME_1",
},
};

describe("ThemeService", () => {
let service: ThemeService;

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
providers: [
{ provide: LocalStorageService, useValue: new MockLocalStorageService() },
{
provide: AppConfigService,
useValue: new MockAppConfigService(MOCK_APP_CONFIG),
},
],
});
service = TestBed.inject(ThemeService);
});

Expand Down
50 changes: 39 additions & 11 deletions src/app/shared/services/skin/skin.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import { AppConfigService } from "../app-config/app-config.service";
import { MockAppConfigService } from "../app-config/app-config.service.spec";
import { TemplateService } from "../../components/template/services/template.service";
import { ThemeService } from "src/app/feature/theme/services/theme.service";
import { MockThemeService } from "src/app/feature/theme/services/theme.service.spec";
import { IAppConfig, IAppSkin } from "packages/data-models";

class MockThemeService implements Partial<ThemeService> {
ready() {
return true;
}
setTheme() {}
getCurrentTheme() {
return "mock_theme";
}
}
import { deepMergeObjects } from "../../utils";
import clone from "clone";

class MockTemplateService implements Partial<TemplateService> {
ready() {
Expand Down Expand Up @@ -51,6 +44,10 @@ const MOCK_APP_CONFIG: Partial<IAppConfig> = {
available: [MOCK_SKIN_1, MOCK_SKIN_2],
defaultSkinName: "MOCK_SKIN_1",
},
APP_THEMES: {
available: ["MOCK_THEME_1", "MOCK_THEME_2"],
defaultThemeName: "MOCK_THEME_1",
},
};

/**
Expand Down Expand Up @@ -85,6 +82,11 @@ describe("SkinService", () => {
expect(service.getActiveSkinName()).toEqual("MOCK_SKIN_1");
});

it("loads active skin from local storage on init if available", () => {
service["localStorageService"].setProtected("APP_SKIN", "MOCK_SKIN_2");
expect(service.getActiveSkinName()).toEqual("MOCK_SKIN_2");
});

it("generates override and revert configs", () => {
expect(service["revertOverride"]).toEqual({
APP_HEADER_DEFAULTS: { title: "default", colour: "none" },
Expand Down Expand Up @@ -116,5 +118,31 @@ describe("SkinService", () => {
});
});

// TODO - add tests for setSkin method and side-effects
it("sets skin: sets active skin name", () => {
service["setSkin"](MOCK_SKIN_2.name);
expect(service.getActiveSkinName()).toEqual("MOCK_SKIN_2");
service["setSkin"](MOCK_SKIN_1.name);
expect(service.getActiveSkinName()).toEqual("MOCK_SKIN_1");
});

it("sets skin: sets revertOverride correctly", () => {
// MOCK_SKIN_1 will already be applied on load
service["setSkin"](MOCK_SKIN_2.name);
expect(service["revertOverride"]).toEqual({
APP_HEADER_DEFAULTS: {
title: "default",
variant: "default",
},
});
});

it("sets skin: updates AppConfigService.appConfig values", () => {
// MOCK_SKIN_1 will already be applied on load
service["setSkin"](MOCK_SKIN_2.name);
expect(service["appConfigService"].appConfig() as Partial<IAppConfig>).toEqual(
deepMergeObjects(clone(MOCK_APP_CONFIG), clone(MOCK_SKIN_2).appConfig)
);
});

// TODO - add further tests for setSkin method and side-effects
});

0 comments on commit 27712b0

Please sign in to comment.