Skip to content

Commit a80211a

Browse files
authored
fix(matchers): toHaveStyle matcher throws TypeError error (#503)
1 parent e25b5eb commit a80211a

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

projects/spectator/jest/test/highlight.directive.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('HighlightDirective', () => {
99
const createHost = createHostFactory(HighlightDirective);
1010

1111
// calculated styles not supported in JSDOM
12-
xit('should change the background color', () => {
12+
it('should change the background color', () => {
1313
host = createHost(`<div highlight>Testing HighlightDirective</div>`);
1414

1515
host.dispatchMouseEvent(host.element, 'mouseover');
@@ -31,7 +31,7 @@ describe('HighlightDirective (createHostDirectiveFactory)', () => {
3131
const createHost = createDirectiveFactory(HighlightDirective);
3232

3333
// calculated styles not supported in JSDOM
34-
xit('should change the background color', () => {
34+
it('should change the background color', () => {
3535
host = createHost(`<div highlight>Testing HighlightDirective</div>`);
3636

3737
host.dispatchMouseEvent(host.element, 'mouseover');

projects/spectator/jest/test/matchers/matchers.spec.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ interface Dummy {
1111
template: `
1212
<div id="display-none" style="display:none">Hidden with display none</div>
1313
<div id="visibility-hidden" style="visibility:hidden">Hidden with visibility hidden</div>
14-
<label>Hidden input element<input type="hidden"/></label>
14+
<label>Hidden input element<input type="hidden" /></label>
1515
<div hidden>Hidden by "hidden"-attribute</div>
1616
<div style="display:none"><div id="parent-display-none">Hidden by parent with display none</div></div>
1717
<div style="visibility:hidden"><div id="parent-visibility-hidden">Hidden by parent with display none</div></div>
1818
<div id="visible">Visible</div>
1919
<div id="classes" class="class-a class-b">Classes</div>
20-
`
20+
<div id="styles" style="background-color: indianred; color: chocolate;"></div>
21+
`,
2122
})
2223
export class MatchersComponent {}
2324

@@ -109,12 +110,34 @@ describe('Matchers', () => {
109110

110111
it('should return true when expected is same as actual', () => {
111112
const actual: Dummy = { lorem: 'first', ipsum: 'second' };
112-
expect(actual).toBePartial({...actual});
113+
expect(actual).toBePartial({ ...actual });
113114
});
114115

115116
it('should return false when expected is not partial of actual', () => {
116117
const actual: Dummy = { lorem: 'first', ipsum: 'second' };
117118
expect(actual).not.toBePartial({ lorem: 'second' });
118119
});
119120
});
121+
122+
describe('toHaveStyle', () => {
123+
it('should return true when expected style exists on element', () => {
124+
const element = spectator.query('#styles');
125+
expect(element).toHaveStyle({ 'background-color': 'indianred' });
126+
});
127+
128+
it('should return true if all styles exist on element', () => {
129+
const element = spectator.query('#styles');
130+
expect(element).toHaveStyle({ 'background-color': 'indianred', color: 'chocolate' });
131+
});
132+
133+
it('should return false if style exists on an element but has a different value than expected', () => {
134+
const element = spectator.query('#styles');
135+
expect(element).not.toHaveStyle({ color: 'blue' });
136+
});
137+
138+
it('should return false when expected style does not exist on element', () => {
139+
const element = spectator.query('#styles');
140+
expect(element).not.toHaveStyle({ height: '100px' });
141+
});
142+
});
120143
});

projects/spectator/src/lib/matchers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const hasCss = (el: HTMLElement, css: { [key: string]: string }) => {
6565
continue;
6666
}
6767

68-
if (trim($el.css(prop)) !== trim(value) && trim(el.style[prop]) !== trim(value)) {
68+
if (trim($el.get(0).style[prop]) !== trim(value) && trim(el.style[prop]) !== trim(value)) {
6969
return false;
7070
}
7171
}

0 commit comments

Comments
 (0)