Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors Theme Component and increase coverage #1340

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions src/app/theme/theme.component.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
<div class="modal-dialog" role="document">
<div class="modal-content">

<div class="modal-header">
<!-- Start ignoring HTMLLintBear -->
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<!-- Stop ignoring HTMLLintBear -->
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
><span aria-hidden="true">&times;</span
></button>
<h4 class="modal-title" id="my-modal-label">Customization</h4>
<button type="button" (click)="setTheme('defaultTheme')" class="btn btn-round btn-default theme-link" id="default">Default</button>
<button type="button" (click)="setTheme('darkTheme')" class="btn btn-round btn-default theme-link" id="dark">Dark</button>
<button type="button" (click)="setTheme('basicTheme')" class="btn btn-round btn-default theme-link" id="basic">Basic</button>
<button type="button" (click)="setTheme('contrastTheme')" class="btn btn-round btn-default theme-link" id="contrast">Contrast</button>
<button type="button" (click)="setTheme('terminalTheme')" class="btn btn-round btn-default theme-link" id="terminal">Terminal</button>
<button type="button" (click)="setTheme('nightTheme')" class="btn btn-round btn-default theme-link" id="night">Night</button>
<button
type="button"
(click)="setTheme('defaultTheme')"
class="btn btn-round btn-default theme-link"
id="default"
>Default</button>
<button
type="button"
(click)="setTheme('darkTheme')"
class="btn btn-round btn-default theme-link"
id="dark"
>Dark</button>
<button
type="button"
(click)="setTheme('basicTheme')"
class="btn btn-round btn-default theme-link"
id="basic"
>Basic</button>
<button
type="button"
(click)="setTheme('contrastTheme')"
class="btn btn-round btn-default theme-link"
id="contrast"
>Contrast</button>
<button
type="button"
(click)="setTheme('terminalTheme')"
class="btn btn-round btn-default theme-link"
id="terminal"
>Terminal</button>
<button
type="button"
(click)="setTheme('nightTheme')"
class="btn btn-round btn-default theme-link"
id="night"
>Night</button>
</div>

</div>
</div>
106 changes: 106 additions & 0 deletions src/app/theme/theme.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ThemeComponent } from './theme.component';
import { ThemeService } from '../services/theme.service';
import { By } from '@angular/platform-browser';

describe('ThemeComponent', () => {
let component: ThemeComponent;
Expand All @@ -28,4 +29,109 @@ describe('ThemeComponent', () => {
it('should be created', () => {
expect(component).toBeTruthy();
});

it('should have modal-title', () => {
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h4.modal-title')).toBeTruthy();
});

it('should have button of class close', () => {
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('button.close')).toBeTruthy();
});

it('should have 6 buttons of class theme-link', () => {
const element = fixture.debugElement.queryAll(By.css('button.theme-link'));
expect(element.length).toBe(6);
});

it('should set default theme', async(() => {
spyOn(component, 'setTheme').and.callThrough();

const element = fixture.debugElement.queryAll(By.css('button.theme-link'))[0];
const btn = element.nativeElement as HTMLElement;
btn.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
const theme = JSON.parse(localStorage.getItem('theme')).value;
expect(theme).toBe('defaultTheme');
});
}));

it('should set dark theme', async(() => {
spyOn(component, 'setTheme').and.callThrough();

const element = fixture.debugElement.queryAll(By.css('button.theme-link'))[1];
const btn = element.nativeElement as HTMLElement;
btn.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok to use then? Can't it be done without it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually whatever reference I got about whenStable with async, I found the same format. I don't think it'll work without this

fixture.detectChanges();
const theme = JSON.parse(localStorage.getItem('theme')).value;
expect(theme).toBe('darkTheme');
});
}));

it('should set basic theme', async(() => {
spyOn(component, 'setTheme').and.callThrough();

const element = fixture.debugElement.queryAll(By.css('button.theme-link'))[2];
const btn = element.nativeElement as HTMLElement;
btn.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
const theme = JSON.parse(localStorage.getItem('theme')).value;
expect(theme).toBe('basicTheme');
});
}));

it('should set contrast theme', async(() => {
spyOn(component, 'setTheme').and.callThrough();

const element = fixture.debugElement.queryAll(By.css('button.theme-link'))[3];
const btn = element.nativeElement as HTMLElement;
btn.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
const theme = JSON.parse(localStorage.getItem('theme')).value;
expect(theme).toBe('contrastTheme');
});
}));

it('should set terminal theme', async(() => {
spyOn(component, 'setTheme').and.callThrough();

const element = fixture.debugElement.queryAll(By.css('button.theme-link'))[4];
const btn = element.nativeElement as HTMLElement;
btn.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
const theme = JSON.parse(localStorage.getItem('theme')).value;
expect(theme).toBe('terminalTheme');
});
}));

it('should set night theme', async(() => {
spyOn(component, 'setTheme').and.callThrough();

const element = fixture.debugElement.queryAll(By.css('button.theme-link'))[5];
const btn = element.nativeElement as HTMLElement;
btn.click();

fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
const theme = JSON.parse(localStorage.getItem('theme')).value;
expect(theme).toBe('nightTheme');
});
}));
});
12 changes: 8 additions & 4 deletions src/app/theme/theme.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,19 @@ export class ThemeComponent implements OnInit {
this.themeService.navbarbgColor = '#373737';
this.themeService.searchbarColor = '#ffffff';
this.themeService.searchbarbgColor = '#323232';
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.background = '#373737';
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.borderTop = '#222222';
if (typeof(document.getElementsByClassName("footer-bar")[0] as HTMLElement) !== 'undefined') {
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.background = '#373737';
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.borderTop = '#222222';
}
setBtnActive('night');
}
}

function setFooter() {
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.background = '#f2f2f2';
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.borderTop = '#e4e4e4';
if (typeof(document.getElementsByClassName("footer-bar")[0] as HTMLElement) !== 'undefined') {
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.background = '#f2f2f2';
(document.getElementsByClassName("footer-bar")[0] as HTMLElement).style.borderTop = '#e4e4e4';
}
}

function setBtnActive(btnID) {
Expand Down