Skip to content

Commit a5b388f

Browse files
authored
test: add integration tests (#138)
Co-authored-by: Daniel Kimmich <[email protected]>
1 parent 0338128 commit a5b388f

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

angular.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@
124124
"options": {
125125
"main": "projects/material-css-vars/src/test.ts",
126126
"tsConfig": "projects/material-css-vars/tsconfig.spec.json",
127-
"karmaConfig": "projects/material-css-vars/karma.conf.js"
127+
"karmaConfig": "projects/material-css-vars/karma.conf.js",
128+
"styles": [
129+
"projects/material-css-vars/src/test/global-styles.scss"
130+
]
128131
}
129132
},
130133
"lint": {

projects/material-css-vars/ng-package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@ctrl/tinycolor"
1010
],
1111
"assets": [
12-
"**/*.scss"
12+
"./index.scss",
13+
"./src/lib/*.scss"
1314
]
1415
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@use '../../index' as mat-css-vars;
2+
3+
@include mat-css-vars.init-material-css-vars;
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import {Component, Input} from '@angular/core';
2+
import {MatButtonModule} from '@angular/material/button';
3+
import {ThemePalette} from '@angular/material/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {MaterialCssVarsModule} from '../lib/material-css-vars.module';
6+
import {By} from '@angular/platform-browser';
7+
8+
@Component({
9+
template: `
10+
<button mat-raised-button [color]="color">Button text</button>`,
11+
standalone: true,
12+
imports: [MatButtonModule],
13+
})
14+
class ButtonComponent {
15+
@Input() color: ThemePalette;
16+
}
17+
18+
describe('integration', () => {
19+
describe('custom colors', () => {
20+
let button: ButtonComponent;
21+
let fixture: ComponentFixture<ButtonComponent>;
22+
23+
beforeEach(() => {
24+
TestBed.configureTestingModule({
25+
imports: [
26+
MaterialCssVarsModule.forRoot({
27+
primary: '#00ff00',
28+
accent: '#0000ff',
29+
warn: '#ff0000',
30+
}),
31+
],
32+
});
33+
fixture = TestBed.createComponent(ButtonComponent);
34+
button = fixture.componentInstance;
35+
});
36+
37+
it('should render a button in the given primary color', () => {
38+
button.color = 'primary';
39+
fixture.detectChanges();
40+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
41+
42+
expect(getComputedStyle(buttonElement).backgroundColor).toEqual('rgb(0, 255, 0)');
43+
});
44+
45+
it('should choose the right contrast color for the primary color', () => {
46+
button.color = 'primary';
47+
fixture.detectChanges();
48+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
49+
50+
expect(getComputedStyle(buttonElement).color).toEqual('rgba(0, 0, 0, 0.87)');
51+
});
52+
53+
it('should render a button in the given accent color', () => {
54+
button.color = 'accent';
55+
fixture.detectChanges();
56+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
57+
58+
expect(getComputedStyle(buttonElement).backgroundColor).toEqual('rgb(0, 0, 255)');
59+
});
60+
61+
it('should choose the right contrast color for the accent color', () => {
62+
button.color = 'accent';
63+
fixture.detectChanges();
64+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
65+
66+
expect(getComputedStyle(buttonElement).color).toEqual('rgb(255, 255, 255)');
67+
});
68+
69+
it('should render a button in the given warn color', () => {
70+
button.color = 'warn';
71+
fixture.detectChanges();
72+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement as HTMLButtonElement;
73+
74+
expect(window.getComputedStyle(buttonElement).backgroundColor).toEqual('rgb(255, 0, 0)');
75+
});
76+
77+
it('should choose the right contrast color for the warn color', () => {
78+
button.color = 'warn';
79+
fixture.detectChanges();
80+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
81+
82+
expect(getComputedStyle(buttonElement).color).toEqual('rgb(255, 255, 255)');
83+
});
84+
});
85+
86+
describe('default colors', () => {
87+
let button: ButtonComponent;
88+
let fixture: ComponentFixture<ButtonComponent>;
89+
90+
beforeEach(() => {
91+
TestBed.configureTestingModule({
92+
imports: [
93+
MaterialCssVarsModule.forRoot({}),
94+
],
95+
});
96+
fixture = TestBed.createComponent(ButtonComponent);
97+
button = fixture.componentInstance;
98+
});
99+
100+
it('should render a button in the default primary color', () => {
101+
button.color = 'primary';
102+
fixture.detectChanges();
103+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
104+
105+
expect(getComputedStyle(buttonElement).backgroundColor).toEqual('rgb(3, 169, 244)');
106+
});
107+
108+
it('should choose the right contrast color for the primary color', () => {
109+
button.color = 'primary';
110+
fixture.detectChanges();
111+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
112+
113+
expect(getComputedStyle(buttonElement).color).toEqual('rgb(255, 255, 255)');
114+
});
115+
116+
it('should render a button in the default accent color', () => {
117+
button.color = 'accent';
118+
fixture.detectChanges();
119+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
120+
121+
expect(getComputedStyle(buttonElement).backgroundColor).toEqual('rgb(233, 30, 99)');
122+
});
123+
124+
it('should choose the right contrast color for the accent color', () => {
125+
button.color = 'accent';
126+
fixture.detectChanges();
127+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
128+
129+
expect(getComputedStyle(buttonElement).color).toEqual('rgb(255, 255, 255)');
130+
});
131+
132+
it('should render a button in the default warn color', () => {
133+
button.color = 'warn';
134+
fixture.detectChanges();
135+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement as HTMLButtonElement;
136+
137+
expect(window.getComputedStyle(buttonElement).backgroundColor).toEqual('rgb(244, 67, 54)');
138+
});
139+
140+
it('should choose the right contrast color for the warn color', () => {
141+
button.color = 'warn';
142+
fixture.detectChanges();
143+
const buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
144+
145+
expect(getComputedStyle(buttonElement).color).toEqual('rgb(255, 255, 255)');
146+
});
147+
});
148+
149+
afterEach(() => {
150+
// clear CSS variables
151+
document.documentElement.removeAttribute('style');
152+
});
153+
});

0 commit comments

Comments
 (0)