Skip to content

Commit

Permalink
feat: add theme local storage repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jonyw4 committed Nov 22, 2021
1 parent 7827c06 commit a2e34fd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
24 changes: 24 additions & 0 deletions webapp/src/infra/ThemeLocalStorageRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ThemeRepository } from "../data";
import { Theme } from "../domain";

const LOCAL_STORAGE_THEME_KEY = "theme";
const DEFAULT_THEME = "light";

export class ThemeLocalStorageRepository implements ThemeRepository {
getTheme(): Theme {
if(!global.localStorage){
return DEFAULT_THEME;
}

const theme = global.localStorage.getItem(LOCAL_STORAGE_THEME_KEY) as Theme;

if(!theme){
return DEFAULT_THEME;
}

return theme;
}
changeTheme(theme: Theme): void {
global.localStorage.setItem(LOCAL_STORAGE_THEME_KEY, theme);
}
}
1 change: 1 addition & 0 deletions webapp/src/infra/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ArticleFileSystemRepository';
export * from './ThemeLocalStorageRepository';
63 changes: 63 additions & 0 deletions webapp/tests/unit/infra/ThemeLocalStorageRepository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ThemeLocalStorageRepository } from '../../../src/infra'
import { createMock } from "ts-auto-mock";

interface Storage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
}

describe("infra :: ThemeLocalStorageRepository", () => {

beforeEach(() => {
// @ts-ignore
global.localStorage = createMock<Storage>();
})

const themeRepository = new ThemeLocalStorageRepository();

describe("givan an undefined localstorage", () => {
beforeEach(() => {
// @ts-ignore
global.localStorage = undefined
});
it("should get the light theme", () => {
const theme = themeRepository.getTheme();
expect(theme).toBe("light");
});
});

describe('given an empty localstorage', () => {
it("should get the light theme", () => {
const theme = themeRepository.getTheme();
expect(theme).toBe("light");
});
});


describe('given an dark theme in localstorage', () => {
beforeEach(() => {
// @ts-ignore
global.localStorage = createMock<Storage>({
getItem: () => "dark"
});
})
it("should get dark theme", () => {
const theme = themeRepository.getTheme();
expect(theme).toBe("dark");
});
});

describe("given an light theme in localstorage", () => {
beforeEach(() => {
// @ts-ignore
global.localStorage = createMock<Storage>({
getItem: () => "light",
});
});
it("should get light theme", () => {
const theme = themeRepository.getTheme();
expect(theme).toBe("light");
});
});

});

0 comments on commit a2e34fd

Please sign in to comment.