Skip to content

Commit b30ccf1

Browse files
committed
test(demo): add component tests
1 parent b0c2a71 commit b30ccf1

File tree

3 files changed

+223
-9
lines changed

3 files changed

+223
-9
lines changed
Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,64 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { By } from '@angular/platform-browser';
23
import { RouterTestingModule } from '@angular/router/testing';
34

45
import { AppComponent } from './app.component';
6+
import { ColorSchemeSwitchComponent } from './components/color-scheme-switch/color-scheme-switch.component';
57

68
describe('AppComponent', () => {
79
let fixture: ComponentFixture<AppComponent>;
810

911
beforeEach(async () => {
1012
await TestBed.configureTestingModule({
11-
imports: [ AppComponent, RouterTestingModule ]
13+
imports: [AppComponent, RouterTestingModule]
1214
}).compileComponents();
1315

1416
fixture = TestBed.createComponent(AppComponent);
1517
fixture.detectChanges();
1618
});
1719

18-
it('should render title', () => {
19-
const compiled = fixture.nativeElement as HTMLElement;
20+
describe('counter', () => {
21+
it('should initially be 0', () => {
22+
expect(fixture.componentInstance.counter()).toBe(0);
23+
});
2024

21-
expect(compiled.querySelector('h1')?.textContent).toContain('@bynary/composables');
25+
it('should be incremented by 1 after calling incrementCounter', () => {
26+
fixture.componentInstance.incrementCounter();
27+
28+
expect(fixture.componentInstance.counter()).toBe(1);
29+
30+
fixture.componentInstance.incrementCounter();
31+
32+
expect(fixture.componentInstance.counter()).toBe(2);
33+
});
2234
});
2335

24-
it(`should have as title 'demo'`, () => {
25-
expect(fixture.componentInstance.title()).toEqual('@bynary/composables');
36+
describe('title', () => {
37+
it(`should have the correct title`, () => {
38+
expect(fixture.componentInstance.title()).toEqual('@bynary/composables');
39+
});
40+
41+
it('should render the title inside a h1', () => {
42+
const h1 = fixture.debugElement.query(By.css('h1'));
43+
44+
expect(h1.nativeElement.textContent).toContain('@bynary/composables');
45+
});
46+
47+
it('should bind the title to the document', () => {
48+
expect(document.title).toContain('@bynary/composables');
49+
});
50+
51+
it('should include the click counter after the first click', () => {
52+
fixture.componentInstance.incrementCounter();
53+
54+
expect(fixture.componentInstance.title()).toEqual('@bynary/composables - Clicks: 1');
55+
});
56+
});
57+
58+
it('should render the color-scheme-switch component', () => {
59+
const predicate = By.directive(ColorSchemeSwitchComponent);
60+
const colorSwitch = fixture.debugElement.query(predicate);
61+
62+
expect(colorSwitch).toBeTruthy();
2663
});
2764
});

apps/demo/src/app/components/button/button.component.spec.ts

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('ButtonComponent', () => {
88

99
beforeEach(async () => {
1010
await TestBed.configureTestingModule({
11-
imports: [ ButtonComponent ]
11+
imports: [ButtonComponent]
1212
}).compileComponents();
1313

1414
fixture = TestBed.createComponent(ButtonComponent);
@@ -19,4 +19,120 @@ describe('ButtonComponent', () => {
1919
it('should create', () => {
2020
expect(component).toBeTruthy();
2121
});
22+
23+
it('should have the correct base class', () => {
24+
expect(fixture.debugElement.classes).toHaveProperty('c-button', true);
25+
});
26+
27+
describe('the `type` property', () => {
28+
it('should have the correct default value', () => {
29+
expect(component.type()).toEqual('button');
30+
});
31+
32+
it('should be changeable', () => {
33+
component.type.set('submit');
34+
35+
expect(component.type()).toEqual('submit');
36+
});
37+
38+
it('should be bound to the `type` attribute on the component', () => {
39+
expect(fixture.debugElement.attributes['type']).toEqual('button');
40+
});
41+
});
42+
43+
describe('the `isDisabled` property', () => {
44+
it('should have the correct initial value', () => {
45+
expect(component.isDisabled()).toBe(false);
46+
});
47+
48+
it('should be changeable', () => {
49+
component.isDisabled.set(true);
50+
51+
expect(component.isDisabled()).toBe(true);
52+
});
53+
54+
it('should be bound to the `disabled` attribute on the component', () => {
55+
expect(fixture.debugElement.attributes['disabled']).toBeFalsy();
56+
57+
component.isDisabled.set(true);
58+
59+
fixture.detectChanges();
60+
61+
expect(fixture.debugElement.attributes['disabled']).toEqual('');
62+
});
63+
64+
it('should be affect the `tabindex` attribute on the component', () => {
65+
expect(fixture.debugElement.attributes['tabindex']).toEqual('0');
66+
67+
component.isDisabled.set(true);
68+
69+
fixture.detectChanges();
70+
71+
expect(fixture.debugElement.attributes['tabindex']).toEqual('-1');
72+
});
73+
});
74+
75+
describe('the `isLoading` property', () => {
76+
it('should have the correct initial value', () => {
77+
expect(component.isLoading()).toBe(false);
78+
});
79+
80+
it('should be changeable', () => {
81+
component.isLoading.set(true);
82+
83+
expect(component.isLoading()).toBe(true);
84+
});
85+
86+
it('should be bound to the `c-button--is-loading` class on the component', () => {
87+
expect(fixture.debugElement.classes).not.toHaveProperty('c-button--is-loading');
88+
89+
component.isLoading.set(true);
90+
91+
fixture.detectChanges();
92+
93+
expect(fixture.debugElement.classes).toHaveProperty('c-button--is-loading', true);
94+
});
95+
});
96+
97+
describe('the `appearance` property', () => {
98+
it('should have the correct initial value', () => {
99+
expect(component.appearance()).toBe('solid');
100+
});
101+
102+
it('should be changeable', () => {
103+
component.appearance.set('outline');
104+
105+
expect(component.appearance()).toBe('outline');
106+
});
107+
108+
it('should be bound as a modifier class on the component', () => {
109+
expect(fixture.debugElement.classes).toHaveProperty('c-button--solid', true);
110+
111+
component.appearance.set('outline');
112+
113+
fixture.detectChanges();
114+
115+
expect(fixture.debugElement.classes).toHaveProperty('c-button--outline', true);
116+
});
117+
});
118+
119+
describe('the `color` property', () => {
120+
it('should have the correct initial value', () => {
121+
expect(component.color()).toBeUndefined();
122+
});
123+
124+
it('should be changeable', () => {
125+
component.color.set('red');
126+
127+
expect(component.color()).toBe('red');
128+
});
129+
130+
it('should be bound as a modifier class on the component', () => {
131+
component.color.set('red');
132+
133+
fixture.detectChanges();
134+
135+
expect(fixture.debugElement.classes).toHaveProperty('c-button--color-red', true);
136+
});
137+
});
22138
});
Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,84 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
23
import { ColorSchemeSwitchComponent } from './color-scheme-switch.component';
34

45
describe('ColorSchemeSwitchComponent', () => {
56
let component: ColorSchemeSwitchComponent;
67
let fixture: ComponentFixture<ColorSchemeSwitchComponent>;
78

8-
99
beforeEach(async () => {
1010
await TestBed.configureTestingModule({
11-
imports: [ ColorSchemeSwitchComponent ]
11+
imports: [ColorSchemeSwitchComponent]
1212
}).compileComponents();
1313

1414
fixture = TestBed.createComponent(ColorSchemeSwitchComponent);
1515
component = fixture.componentInstance;
1616
fixture.detectChanges();
1717
});
1818

19+
afterEach(() => {
20+
localStorage.clear();
21+
});
1922

2023
it('should create', () => {
2124
expect(component).toBeTruthy();
2225
});
26+
27+
describe('colorScheme', () => {
28+
it('should have the correct default value', () => {
29+
expect(component.colorScheme.preferred()).toBeNull();
30+
expect(component.colorScheme.store()).toBeUndefined();
31+
expect(component.colorScheme.resolved()).toEqual('light');
32+
});
33+
34+
it('should be changeable', () => {
35+
component.colorScheme.store.set('dark');
36+
37+
expect(component.colorScheme.resolved()).toEqual('dark');
38+
});
39+
40+
it('should be bound to the `color-scheme` attribute on the component', () => {
41+
expect((document.firstElementChild as HTMLElement).getAttribute('color-scheme')).toEqual('light');
42+
43+
component.colorScheme.store.set('dark');
44+
45+
fixture.detectChanges();
46+
47+
expect((document.firstElementChild as HTMLElement).getAttribute('color-scheme')).toEqual('dark');
48+
});
49+
50+
it('should store the value in localStorage', () => {
51+
expect(localStorage.getItem('color-scheme')).toEqual(null);
52+
53+
component.colorScheme.store.set('dark');
54+
fixture.detectChanges();
55+
56+
expect(localStorage.getItem('color-scheme')).toEqual('dark');
57+
});
58+
});
59+
60+
describe('onClick', () => {
61+
it('should toggle the color scheme', () => {
62+
expect(component.colorScheme.resolved()).toEqual('light');
63+
64+
fixture.debugElement.triggerEventHandler('click');
65+
fixture.detectChanges();
66+
67+
expect(component.colorScheme.resolved()).toEqual('dark');
68+
69+
fixture.debugElement.triggerEventHandler('click');
70+
fixture.detectChanges();
71+
72+
expect(component.colorScheme.resolved()).toEqual('light');
73+
});
74+
});
75+
76+
it('should display a matching emoji for the current color scheme', () => {
77+
expect(fixture.debugElement.nativeElement.textContent).toEqual('🌝');
78+
79+
component.colorScheme.store.set('dark');
80+
fixture.detectChanges();
81+
82+
expect(fixture.debugElement.nativeElement.textContent).toEqual('🌚');
83+
});
2384
});

0 commit comments

Comments
 (0)