Skip to content

Commit a61a094

Browse files
test: use toWarnDev Jest matcher
1 parent d8b339c commit a61a094

File tree

4 files changed

+22
-38
lines changed

4 files changed

+22
-38
lines changed

src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/breadcrumb/
6060
}
6161

6262
// when there is an identical hierarchicalFacets into current configuration
63-
{
64-
const spy = jest.spyOn(global.console, 'warn');
63+
expect(() => {
6564
const config = widget.getConfiguration({
6665
hierarchicalFacets: [{ name: 'category' }],
6766
});
67+
6868
expect(config).toEqual({});
69-
expect(spy).toHaveBeenCalled();
70-
spy.mockReset();
71-
spy.mockRestore();
72-
}
69+
}).toWarnDev();
7370

7471
// when there is already a different hierarchicalFacets into current configuration
7572
{

src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
7272
}
7373

7474
// when there is an identical hierarchicalFacets into current configuration
75-
{
76-
const spy = jest.spyOn(global.console, 'warn');
75+
expect(() => {
7776
const config = widget.getConfiguration({
7877
hierarchicalFacets: [{ name: 'category' }],
7978
});
79+
8080
expect(config).toEqual({});
81-
expect(spy).toHaveBeenCalled();
82-
spy.mockReset();
83-
spy.mockRestore();
84-
}
81+
}).toWarnDev();
8582

8683
// when there is already a different hierarchicalFacets into current configuration
8784
{

src/widgets/hits-per-page/__tests__/hits-per-page-test.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ describe('hitsPerPage()', () => {
2828
let widget;
2929
let helper;
3030
let results;
31-
let consoleWarn;
3231
let state;
3332

3433
beforeEach(() => {
35-
consoleWarn = jest.spyOn(window.console, 'warn');
36-
3734
container = document.createElement('div');
3835
items = [
3936
{ value: 10, label: '10 results' },
@@ -117,22 +114,24 @@ describe('hitsPerPage()', () => {
117114
expect(helper.search).toHaveBeenCalledTimes(1, 'search called once');
118115
});
119116

120-
it('should throw if there is no name attribute in a passed object', () => {
117+
it('should warn without name attribute in a passed item', () => {
121118
items.length = 0;
122119
items.push({ label: 'Label without a value' });
123-
widget.init({ state: helper.state, helper });
124-
expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once');
125-
expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
126-
`"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: 20)"`
120+
121+
expect(() => {
122+
widget.init({ state: helper.state, helper });
123+
}).toWarnDev(
124+
'[InstantSearch.js]: No items in HitsPerPage `items` with `value: hitsPerPage` (hitsPerPage: 20)'
127125
);
128126
});
129127

130128
it('must include the current hitsPerPage at initialization time', () => {
131129
helper.state.hitsPerPage = -1;
132-
widget.init({ state: helper.state, helper });
133-
expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once');
134-
expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
135-
`"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: -1)"`
130+
131+
expect(() => {
132+
widget.init({ state: helper.state, helper });
133+
}).toWarnDev(
134+
'[InstantSearch.js]: No items in HitsPerPage `items` with `value: hitsPerPage` (hitsPerPage: -1)'
136135
);
137136
});
138137

@@ -142,8 +141,4 @@ describe('hitsPerPage()', () => {
142141
widget.init({ state: helper.state, helper });
143142
}).not.toThrow(/No item in `items`/);
144143
});
145-
146-
afterEach(() => {
147-
consoleWarn.mockRestore();
148-
});
149144
});

src/widgets/panel/__tests__/panel-test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@ describe('Usage', () => {
2222
});
2323

2424
test('with `hidden` as boolean warns', () => {
25-
const warn = jest.spyOn(global.console, 'warn');
26-
warn.mockImplementation(() => {});
27-
28-
panel({
29-
hidden: true,
30-
});
31-
32-
expect(warn).toHaveBeenCalledWith(
25+
expect(() => {
26+
panel({
27+
hidden: true,
28+
});
29+
}).toWarnDev(
3330
'[InstantSearch.js]: The `hidden` option in the "panel" widget expects a function returning a boolean (received "boolean" type).'
3431
);
35-
36-
warn.mockRestore();
3732
});
3833

3934
test('with a widget without `container` throws', () => {

0 commit comments

Comments
 (0)