Skip to content

Commit 71734a0

Browse files
committed
test(flex-layout): migrate custom matchers to Vitest
1 parent d37e7cc commit 71734a0

File tree

24 files changed

+704
-692
lines changed

24 files changed

+704
-692
lines changed

projects/libs/flex-layout/_private-utils/testing/custom-matchers.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1-
declare var global: any;
2-
const _global = <any>(typeof window === 'undefined' ? global : window);
3-
4-
import { _dom as _ } from './dom-tools';
5-
61
import {
72
applyCssPrefixes,
83
extendObject,
94
} from '@ngbracket/ngx-layout/_private-utils';
105
import { StyleUtils } from '@ngbracket/ngx-layout/core';
6+
// This line is important even if imported values are not used! See https://stackoverflow.com/a/78524129/1071200
7+
import type { Assertion, AsymmetricMatchersContaining } from 'vitest';
8+
import { expect } from 'vitest';
119

12-
export const expect: (actual: any) => NgMatchers = <any>_global.expect;
10+
declare module 'vitest' {
11+
interface Assertion<T = any> extends NgMatchers<T> {}
12+
interface AsymmetricMatchersContaining extends NgMatchers {}
13+
}
14+
15+
interface ExpectationResult {
16+
pass: boolean;
17+
message: () => string;
18+
// If you pass these, they will automatically appear inside a diff when
19+
// the matcher does not pass, so you don't need to print the diff yourself
20+
actual?: unknown;
21+
expected?: unknown;
22+
}
1323

1424
/**
1525
* Jasmine matchers that check Angular specific conditions.
1626
*/
17-
export interface NgMatchers extends jasmine.Matchers<any> {
27+
// export interface NgMatchers extends jasmine.Matchers<any> {
28+
export interface NgMatchers<R = unknown> {
1829
/**
1930
* Expect the element to have the given CSS styles injected INLINE
2031
*
2132
* ## Example
2233
*
2334
* {@example testing/ts/matchers.ts region='toHaveStyle'}
2435
*/
25-
toHaveStyle(expected: { [k: string]: string } | string): boolean;
36+
toHaveInlineStyle(
37+
expected: { [k: string]: string } | string,
38+
styler: StyleUtils
39+
): R;
2640

2741
/**
2842
* Expect the element to have the given CSS inline OR computed styles.
@@ -31,39 +45,36 @@ export interface NgMatchers extends jasmine.Matchers<any> {
3145
*
3246
* {@example testing/ts/matchers.ts region='toHaveStyle'}
3347
*/
34-
toHaveCSS(expected: { [k: string]: string } | string): boolean;
48+
toHaveCSS(expected: { [k: string]: string } | string, styler: StyleUtils): R;
3549

36-
/**
37-
* Invert the matchers.
38-
*/
39-
not: NgMatchers;
50+
// /**
51+
// * Invert the matchers.
52+
// */
53+
// not: NgMatchers;
4054
}
4155

4256
/**
4357
* NOTE: These custom JASMINE Matchers are used only
4458
* in the Karma/Jasmine testing for the Layout Directives
4559
* in `src/lib/flex/api`
4660
*/
47-
export const customMatchers: jasmine.CustomMatcherFactories = {
61+
export const customMatchers: Record<
62+
string,
63+
(received: any, ...expected: any[]) => ExpectationResult
64+
> = {
4865
/**
4966
* Check element's inline styles only
5067
*/
51-
toHaveStyle: function () {
52-
return {
53-
compare: buildCompareStyleFunction(true),
54-
};
55-
},
68+
toHaveInlineStyle: buildCompareStyleFunction(true),
5669

5770
/**
5871
* Check element's css stylesheet only (if not present inline)
5972
*/
60-
toHaveCSS: function () {
61-
return {
62-
compare: buildCompareStyleFunction(false),
63-
};
64-
},
73+
toHaveCSS: buildCompareStyleFunction(false),
6574
};
6675

76+
expect.extend(customMatchers);
77+
6778
/**
6879
* Curried value to function to check styles that are inline or in a stylesheet for the
6980
* specified DOM element.
@@ -73,7 +84,7 @@ function buildCompareStyleFunction(inlineOnly = true) {
7384
actual: any,
7485
styles: { [k: string]: string } | string,
7586
styler: StyleUtils
76-
) {
87+
): ExpectationResult {
7788
const found = {};
7889
const styleMap: { [k: string]: string } = {};
7990

@@ -100,7 +111,7 @@ function buildCompareStyleFunction(inlineOnly = true) {
100111

101112
return {
102113
pass: allPassed,
103-
get message() {
114+
message() {
104115
const expectedValueStr =
105116
typeof styles === 'string'
106117
? styleMap

projects/libs/flex-layout/core/style-utils/style-utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('styler', () => {
5050
componentWithTemplate(`
5151
<div></div>
5252
`);
53-
expectNativeEl(fixture).not.toHaveStyle({ display: 'block' }, styler);
53+
expectNativeEl(fixture).not.toHaveInlineStyle({ display: 'block' }, styler);
5454
});
5555

5656
it('should find to "display" for inline style <div></div>', () => {

projects/libs/flex-layout/extended/img-src/img-src.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('img-src directive', () => {
120120

121121
fixture.detectChanges();
122122
if (isPlatformServer(platformId)) {
123-
expectEl(img).toHaveStyle(
123+
expectEl(img).toHaveInlineStyle(
124124
{
125125
content: 'url(https://dummyimage.com/300x300/c72538/ffffff.png)',
126126
},
@@ -130,7 +130,7 @@ describe('img-src directive', () => {
130130
let url = 'https://dummyimage.com/700x400/258cc7/fff.png';
131131
fixture.componentInstance.defaultSrc = url;
132132
fixture.detectChanges();
133-
expectEl(img).toHaveStyle(
133+
expectEl(img).toHaveInlineStyle(
134134
{
135135
content: `url(${url})`,
136136
},
@@ -174,7 +174,7 @@ describe('img-src directive', () => {
174174

175175
const img = queryFor(fixture, 'img')[0];
176176
fixture.detectChanges();
177-
expectEl(img).not.toHaveStyle(
177+
expectEl(img).not.toHaveInlineStyle(
178178
{
179179
content: 'url(https://dummyimage.com/300x300/c72538/ffffff.png)',
180180
},
@@ -194,7 +194,7 @@ describe('img-src directive', () => {
194194

195195
const img = queryFor(fixture, 'img')[0];
196196

197-
expectEl(img).toHaveStyle(
197+
expectEl(img).toHaveInlineStyle(
198198
{
199199
content: `url(https://dummyimage.com/300x300/c72538/ffffff.png)`,
200200
},
@@ -203,7 +203,7 @@ describe('img-src directive', () => {
203203

204204
mediaController.activate('md');
205205
fixture.detectChanges();
206-
expectEl(img).toHaveStyle(
206+
expectEl(img).toHaveInlineStyle(
207207
{
208208
content: `url(${SRC_URLS['md'][0]})`,
209209
},
@@ -226,7 +226,7 @@ describe('img-src directive', () => {
226226
fixture.detectChanges();
227227
expect(imgEl).toBeDefined();
228228
if (isPlatformServer(platformId)) {
229-
expectEl(img).toHaveStyle(
229+
expectEl(img).toHaveInlineStyle(
230230
{
231231
content: `url(${SRC_URLS['md'][0]})`,
232232
},
@@ -236,7 +236,7 @@ describe('img-src directive', () => {
236236
// When activating an unused breakpoint, fallback to default [src] value
237237
mediaController.activate('xl');
238238
fixture.detectChanges();
239-
expectEl(img).toHaveStyle(
239+
expectEl(img).toHaveInlineStyle(
240240
{
241241
content: `url(${SRC_URLS['xs'][0]})`,
242242
},
@@ -264,7 +264,7 @@ describe('img-src directive', () => {
264264
let imgEl = img.nativeElement;
265265
expect(imgEl).toBeDefined();
266266
if (isPlatformServer(platformId)) {
267-
expectEl(img).toHaveStyle(
267+
expectEl(img).toHaveInlineStyle(
268268
{
269269
content: `url(${defaultSrc})`,
270270
},
@@ -273,7 +273,7 @@ describe('img-src directive', () => {
273273

274274
mediaController.activate('xs');
275275
fixture.detectChanges();
276-
expectEl(img).toHaveStyle(
276+
expectEl(img).toHaveInlineStyle(
277277
{
278278
content: `url(${xsSrc})`,
279279
},
@@ -282,7 +282,7 @@ describe('img-src directive', () => {
282282

283283
mediaController.activate('lg');
284284
fixture.detectChanges();
285-
expectEl(img).toHaveStyle(
285+
expectEl(img).toHaveInlineStyle(
286286
{
287287
content: `url(${defaultSrc})`,
288288
},
@@ -314,7 +314,7 @@ describe('img-src directive', () => {
314314
expect(imgEl).toBeDefined();
315315
if (isPlatformServer(platformId)) {
316316
expect(imgEl).toHaveAttribute('src', '');
317-
expectEl(img).toHaveStyle(
317+
expectEl(img).toHaveInlineStyle(
318318
{
319319
content: `url(${SRC_URLS['md'][0]})`,
320320
},
@@ -324,7 +324,7 @@ describe('img-src directive', () => {
324324
// When activating an unused breakpoint, fallback to default [src] value
325325
mediaController.activate('xl');
326326
fixture.detectChanges();
327-
expectEl(img).not.toHaveStyle(
327+
expectEl(img).not.toHaveInlineStyle(
328328
{
329329
content: `url(${SRC_URLS['md'][0]})`,
330330
},

0 commit comments

Comments
 (0)