-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add theme local storage repository
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
webapp/tests/unit/infra/ThemeLocalStorageRepository.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
|
||
}); |