Skip to content

Commit c63a8a0

Browse files
committed
refactor: TT-332 application of Azure toggle function to display dark mode
1 parent 2d648d7 commit c63a8a0

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<button
22
class="w-10 h-6 rounded-full bg-gray-lightest flex items-center transition duration-300 focus:outline-none shadow"
33
(click)="changeToDarkOrLightTheme()"
4+
*ngIf="isFeatureToggleDarkModeActive"
45
>
56
<div
67
#themeToggle

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { CookieService } from 'ngx-cookie-service';
3+
import { FeatureToggle } from 'src/environments/enum';
24

35
import { DarkModeComponent } from './dark-mode.component';
46

57
describe('DarkModeComponent', () => {
68
let component: DarkModeComponent;
79
let fixture: ComponentFixture<DarkModeComponent>;
10+
let cookieService: CookieService;
811

912
beforeEach(async () => {
1013
await TestBed.configureTestingModule({
@@ -14,6 +17,7 @@ describe('DarkModeComponent', () => {
1417

1518
beforeEach(() => {
1619
fixture = TestBed.createComponent(DarkModeComponent);
20+
cookieService = TestBed.inject(CookieService);
1721
component = fixture.componentInstance;
1822
fixture.detectChanges();
1923
});
@@ -26,32 +30,32 @@ describe('DarkModeComponent', () => {
2630
expect(component).toBeTruthy();
2731
});
2832

29-
it('should be clear the default theme', () => {
30-
expect(component.theme).toBe('light');
33+
it('should be light the default theme', () => {
34+
expect(component.theme).toEqual('light');
3135
});
3236

3337
it('should change the value of the theme property if it exists in the local storage', () => {
3438
localStorage.setItem('theme', 'dark');
3539
component.checkThemeInLocalStorage();
36-
expect(component.theme).toBe('dark');
40+
expect(component.theme).toEqual('dark');
3741
});
3842

3943
it('should be light theme if it does not exist in local storage', () => {
4044
component.checkThemeInLocalStorage();
41-
expect(component.theme).toBe('light');
45+
expect(component.theme).toEqual('light');
4246
});
4347

4448
it('should be light theme if only the theme property exists in local storage and not its value', () => {
4549
localStorage.setItem('theme', '');
4650
component.checkThemeInLocalStorage();
47-
expect(component.theme).toBe('light');
51+
expect(component.theme).toEqual('light');
4852
});
4953

5054
it('should switch to dark theme if the theme property is light and vice versa', () => {
5155
component.theme = component.setTheme();
52-
expect(component.theme).toBe('dark');
56+
expect(component.theme).toEqual('dark');
5357
component.theme = component.setTheme();
54-
expect(component.theme).toBe('light');
58+
expect(component.theme).toEqual('light');
5559
});
5660

5761
it('should add the dark class in the html tag to apply dark mode', () => {
@@ -63,9 +67,19 @@ describe('DarkModeComponent', () => {
6367
});
6468

6569
it('should be changed to dark mode if the mode toggle is selected', () => {
70+
component.isFeatureToggleDarkModeActive = true;
71+
fixture.detectChanges();
6672
component.themeToggle.nativeElement.click();
6773
fixture.detectChanges();
68-
expect(component.theme).toBe('dark');
69-
expect(localStorage.getItem('theme')).toBe('dark');
74+
expect(component.theme).toEqual('dark');
75+
expect(localStorage.getItem('theme')).toEqual('dark');
76+
});
77+
78+
it('call switchThemeToggleStyles() when ngAfterViewInit is called and isFeatureToggleDarkModeActive is true', () => {
79+
component.isFeatureToggleDarkModeActive = true;
80+
fixture.detectChanges();
81+
component.ngAfterViewInit();
82+
fixture.detectChanges();
83+
expect(component.themeToggle.nativeElement.classList.contains('bg-warning')).toBe(true);
7084
});
7185
});

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
import { AfterViewInit, ElementRef, ViewChild, Component } from '@angular/core';
1+
import { AfterViewInit, ElementRef, ViewChild, Component, OnInit } from '@angular/core';
2+
import { CookieService } from 'ngx-cookie-service';
3+
import { FeatureToggle } from 'src/environments/enum';
4+
import { FeatureToggleGeneralService } from '../../feature-toggles/feature-toggle-general/feature-toggle-general.service';
25

36
@Component({
47
selector: 'app-dark-mode',
58
templateUrl: './dark-mode.component.html',
69
styleUrls: ['./dark-mode.component.scss'],
710
})
8-
export class DarkModeComponent implements AfterViewInit {
11+
export class DarkModeComponent implements OnInit, AfterViewInit {
912
public theme = 'light';
13+
public isFeatureToggleDarkModeActive: boolean;
1014

1115
@ViewChild('themeToggle') themeToggle: ElementRef;
1216

13-
constructor() {
14-
this.checkThemeInLocalStorage();
15-
this.addOrRemoveDarkMode();
17+
constructor(
18+
private cookiesService: CookieService,
19+
private featureToggleGeneralService: FeatureToggleGeneralService
20+
) {}
21+
22+
ngOnInit(): void {
23+
this.featureToggleGeneralService.getActivated().subscribe(() => {
24+
this.isFeatureToggleDarkModeActive = this.cookiesService.get(FeatureToggle.DARK_MODE) === 'true';
25+
});
26+
if (this.isFeatureToggleDarkModeActive) {
27+
this.checkThemeInLocalStorage();
28+
this.addOrRemoveDarkMode();
29+
}
1630
}
1731

1832
ngAfterViewInit(): void {
19-
this.switchThemeToggleStyles(this.theme);
33+
if (this.isFeatureToggleDarkModeActive) {
34+
this.switchThemeToggleStyles(this.theme);
35+
}
2036
}
2137

2238
getLocalStorageTheme(): string {

src/environments/enum.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum FeatureToggle {
2-
SWITCH_GROUP = 'switch-group',
3-
TIME_TRACKER_CALENDAR = 'time-tracker-calendar'
2+
SWITCH_GROUP = 'switch-group',
3+
TIME_TRACKER_CALENDAR = 'time-tracker-calendar',
4+
DARK_MODE = 'dark-mode',
45
}

0 commit comments

Comments
 (0)