Skip to content

Commit

Permalink
test: use toWarnDev Jest matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Mar 3, 2019
1 parent d8b339c commit a61a094
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
9 changes: 3 additions & 6 deletions src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/breadcrumb/
}

// when there is an identical hierarchicalFacets into current configuration
{
const spy = jest.spyOn(global.console, 'warn');
expect(() => {
const config = widget.getConfiguration({
hierarchicalFacets: [{ name: 'category' }],
});

expect(config).toEqual({});
expect(spy).toHaveBeenCalled();
spy.mockReset();
spy.mockRestore();
}
}).toWarnDev();

// when there is already a different hierarchicalFacets into current configuration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
}

// when there is an identical hierarchicalFacets into current configuration
{
const spy = jest.spyOn(global.console, 'warn');
expect(() => {
const config = widget.getConfiguration({
hierarchicalFacets: [{ name: 'category' }],
});

expect(config).toEqual({});
expect(spy).toHaveBeenCalled();
spy.mockReset();
spy.mockRestore();
}
}).toWarnDev();

// when there is already a different hierarchicalFacets into current configuration
{
Expand Down
27 changes: 11 additions & 16 deletions src/widgets/hits-per-page/__tests__/hits-per-page-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ describe('hitsPerPage()', () => {
let widget;
let helper;
let results;
let consoleWarn;
let state;

beforeEach(() => {
consoleWarn = jest.spyOn(window.console, 'warn');

container = document.createElement('div');
items = [
{ value: 10, label: '10 results' },
Expand Down Expand Up @@ -117,22 +114,24 @@ describe('hitsPerPage()', () => {
expect(helper.search).toHaveBeenCalledTimes(1, 'search called once');
});

it('should throw if there is no name attribute in a passed object', () => {
it('should warn without name attribute in a passed item', () => {
items.length = 0;
items.push({ label: 'Label without a value' });
widget.init({ state: helper.state, helper });
expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once');
expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
`"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: 20)"`

expect(() => {
widget.init({ state: helper.state, helper });
}).toWarnDev(
'[InstantSearch.js]: No items in HitsPerPage `items` with `value: hitsPerPage` (hitsPerPage: 20)'
);
});

it('must include the current hitsPerPage at initialization time', () => {
helper.state.hitsPerPage = -1;
widget.init({ state: helper.state, helper });
expect(consoleWarn).toHaveBeenCalledTimes(1, 'console.warn called once');
expect(consoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(
`"[InstantSearch.js]: No items in HitsPerPage \`items\` with \`value: hitsPerPage\` (hitsPerPage: -1)"`

expect(() => {
widget.init({ state: helper.state, helper });
}).toWarnDev(
'[InstantSearch.js]: No items in HitsPerPage `items` with `value: hitsPerPage` (hitsPerPage: -1)'
);
});

Expand All @@ -142,8 +141,4 @@ describe('hitsPerPage()', () => {
widget.init({ state: helper.state, helper });
}).not.toThrow(/No item in `items`/);
});

afterEach(() => {
consoleWarn.mockRestore();
});
});
15 changes: 5 additions & 10 deletions src/widgets/panel/__tests__/panel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ describe('Usage', () => {
});

test('with `hidden` as boolean warns', () => {
const warn = jest.spyOn(global.console, 'warn');
warn.mockImplementation(() => {});

panel({
hidden: true,
});

expect(warn).toHaveBeenCalledWith(
expect(() => {
panel({
hidden: true,
});
}).toWarnDev(
'[InstantSearch.js]: The `hidden` option in the "panel" widget expects a function returning a boolean (received "boolean" type).'
);

warn.mockRestore();
});

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

0 comments on commit a61a094

Please sign in to comment.