-
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.
- Loading branch information
Showing
3 changed files
with
223 additions
and
9 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 |
---|---|---|
@@ -1,27 +1,64 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
|
||
import { AppComponent } from './app.component'; | ||
import { ColorSchemeSwitchComponent } from './components/color-scheme-switch/color-scheme-switch.component'; | ||
|
||
describe('AppComponent', () => { | ||
let fixture: ComponentFixture<AppComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ AppComponent, RouterTestingModule ] | ||
imports: [AppComponent, RouterTestingModule] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(AppComponent); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should render title', () => { | ||
const compiled = fixture.nativeElement as HTMLElement; | ||
describe('counter', () => { | ||
it('should initially be 0', () => { | ||
expect(fixture.componentInstance.counter()).toBe(0); | ||
}); | ||
|
||
expect(compiled.querySelector('h1')?.textContent).toContain('@bynary/composables'); | ||
it('should be incremented by 1 after calling incrementCounter', () => { | ||
fixture.componentInstance.incrementCounter(); | ||
|
||
expect(fixture.componentInstance.counter()).toBe(1); | ||
|
||
fixture.componentInstance.incrementCounter(); | ||
|
||
expect(fixture.componentInstance.counter()).toBe(2); | ||
}); | ||
}); | ||
|
||
it(`should have as title 'demo'`, () => { | ||
expect(fixture.componentInstance.title()).toEqual('@bynary/composables'); | ||
describe('title', () => { | ||
it(`should have the correct title`, () => { | ||
expect(fixture.componentInstance.title()).toEqual('@bynary/composables'); | ||
}); | ||
|
||
it('should render the title inside a h1', () => { | ||
const h1 = fixture.debugElement.query(By.css('h1')); | ||
|
||
expect(h1.nativeElement.textContent).toContain('@bynary/composables'); | ||
}); | ||
|
||
it('should bind the title to the document', () => { | ||
expect(document.title).toContain('@bynary/composables'); | ||
}); | ||
|
||
it('should include the click counter after the first click', () => { | ||
fixture.componentInstance.incrementCounter(); | ||
|
||
expect(fixture.componentInstance.title()).toEqual('@bynary/composables - Clicks: 1'); | ||
}); | ||
}); | ||
|
||
it('should render the color-scheme-switch component', () => { | ||
const predicate = By.directive(ColorSchemeSwitchComponent); | ||
const colorSwitch = fixture.debugElement.query(predicate); | ||
|
||
expect(colorSwitch).toBeTruthy(); | ||
}); | ||
}); |
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
65 changes: 63 additions & 2 deletions
65
apps/demo/src/app/components/color-scheme-switch/color-scheme-switch.component.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 |
---|---|---|
@@ -1,23 +1,84 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ColorSchemeSwitchComponent } from './color-scheme-switch.component'; | ||
|
||
describe('ColorSchemeSwitchComponent', () => { | ||
let component: ColorSchemeSwitchComponent; | ||
let fixture: ComponentFixture<ColorSchemeSwitchComponent>; | ||
|
||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ ColorSchemeSwitchComponent ] | ||
imports: [ColorSchemeSwitchComponent] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(ColorSchemeSwitchComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
afterEach(() => { | ||
localStorage.clear(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
describe('colorScheme', () => { | ||
it('should have the correct default value', () => { | ||
expect(component.colorScheme.preferred()).toBeNull(); | ||
expect(component.colorScheme.store()).toBeUndefined(); | ||
expect(component.colorScheme.resolved()).toEqual('light'); | ||
}); | ||
|
||
it('should be changeable', () => { | ||
component.colorScheme.store.set('dark'); | ||
|
||
expect(component.colorScheme.resolved()).toEqual('dark'); | ||
}); | ||
|
||
it('should be bound to the `color-scheme` attribute on the component', () => { | ||
expect((document.firstElementChild as HTMLElement).getAttribute('color-scheme')).toEqual('light'); | ||
|
||
component.colorScheme.store.set('dark'); | ||
|
||
fixture.detectChanges(); | ||
|
||
expect((document.firstElementChild as HTMLElement).getAttribute('color-scheme')).toEqual('dark'); | ||
}); | ||
|
||
it('should store the value in localStorage', () => { | ||
expect(localStorage.getItem('color-scheme')).toEqual(null); | ||
|
||
component.colorScheme.store.set('dark'); | ||
fixture.detectChanges(); | ||
|
||
expect(localStorage.getItem('color-scheme')).toEqual('dark'); | ||
}); | ||
}); | ||
|
||
describe('onClick', () => { | ||
it('should toggle the color scheme', () => { | ||
expect(component.colorScheme.resolved()).toEqual('light'); | ||
|
||
fixture.debugElement.triggerEventHandler('click'); | ||
fixture.detectChanges(); | ||
|
||
expect(component.colorScheme.resolved()).toEqual('dark'); | ||
|
||
fixture.debugElement.triggerEventHandler('click'); | ||
fixture.detectChanges(); | ||
|
||
expect(component.colorScheme.resolved()).toEqual('light'); | ||
}); | ||
}); | ||
|
||
it('should display a matching emoji for the current color scheme', () => { | ||
expect(fixture.debugElement.nativeElement.textContent).toEqual('🌝'); | ||
|
||
component.colorScheme.store.set('dark'); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.debugElement.nativeElement.textContent).toEqual('🌚'); | ||
}); | ||
}); |