Skip to content

Commit 2d648d7

Browse files
committed
test: TT-332 unit test on the dark mode component
1 parent b017fbd commit 2d648d7

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/app/modules/shared/components/dark-mode/dark-mode.component.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ describe('DarkModeComponent', () => {
4141
expect(component.theme).toBe('light');
4242
});
4343

44+
it('should be light theme if only the theme property exists in local storage and not its value', () => {
45+
localStorage.setItem('theme', '');
46+
component.checkThemeInLocalStorage();
47+
expect(component.theme).toBe('light');
48+
});
49+
50+
it('should switch to dark theme if the theme property is light and vice versa', () => {
51+
component.theme = component.setTheme();
52+
expect(component.theme).toBe('dark');
53+
component.theme = component.setTheme();
54+
expect(component.theme).toBe('light');
55+
});
56+
57+
it('should add the dark class in the html tag to apply dark mode', () => {
58+
component.theme = 'dark';
59+
component.addOrRemoveDarkMode();
60+
fixture.detectChanges();
61+
const htmlContainsDarkClass = document.documentElement.classList.contains('dark');
62+
expect(htmlContainsDarkClass).toBe(true);
63+
});
64+
4465
it('should be changed to dark mode if the mode toggle is selected', () => {
4566
component.themeToggle.nativeElement.click();
4667
fixture.detectChanges();

src/app/modules/shared/components/dark-mode/dark-mode.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export class DarkModeComponent implements AfterViewInit {
3939
return this.theme === 'dark' ? true : false;
4040
}
4141

42+
setTheme(): string {
43+
return this.isDarkTheme() ? 'light' : 'dark';
44+
}
45+
4246
addOrRemoveDarkMode(): void {
4347
if (this.isDarkTheme()) {
4448
document.documentElement.classList.add('dark');
@@ -48,11 +52,7 @@ export class DarkModeComponent implements AfterViewInit {
4852
}
4953

5054
changeToDarkOrLightTheme(): void {
51-
if (this.isDarkTheme()) {
52-
this.theme = 'light';
53-
} else {
54-
this.theme = 'dark';
55-
}
55+
this.theme = this.setTheme();
5656
this.setLocalStorageTheme(this.theme);
5757
this.switchThemeToggleStyles(this.theme);
5858
this.addOrRemoveDarkMode();

0 commit comments

Comments
 (0)