Skip to content

Commit

Permalink
chore(core): move tests to core (#6463)
Browse files Browse the repository at this point in the history
these were still in instantsearch.js but actually are for the "core" domain now.
  • Loading branch information
Haroenv authored Dec 9, 2024
1 parent a9b623e commit 4f82be2
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';
import qs from 'qs';

import instantsearch from '../..';
import { createWidget } from '../../../test/createWidget';
import { connectHitsPerPage, connectSearchBox } from '../../connectors';
import historyRouter from '../routers/history';

import type { Router, UiState, StateMapping, IndexUiState } from '../../types';
import {
instantsearch,
historyRouter,
connectHitsPerPage,
connectSearchBox,
} from '..';
import { createWidget } from '../../test/createWidget';

import type { Router, UiState, StateMapping, IndexUiState } from '../types';
import type { JSDOM } from 'jsdom';

declare const jsdom: JSDOM;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import { castToJestMock } from '@instantsearch/testutils';
import { wait } from '@instantsearch/testutils/wait';
import { getByText, fireEvent } from '@testing-library/dom';

import { connectConfigure, connectSearchBox } from '../../connectors';
import instantsearch from '../../index.es';
import { configure, frequentlyBoughtTogether, searchBox } from '../../widgets';
import {
instantsearch,
connectConfigure,
connectSearchBox,
connectFrequentlyBoughtTogether,
} from '..';

import type { MiddlewareDefinition } from '../../types';
import type { MiddlewareDefinition } from '../types';

describe('configure', () => {
it('provides up-to-date uiState to onStateChange', () => {
Expand All @@ -38,8 +41,10 @@ describe('configure', () => {
}
});
search.addWidgets([
configure({
hitsPerPage: 10,
connectConfigure(() => {})({
searchParameters: {
hitsPerPage: 10,
},
}),
customComp({ searchParameters: {} }),
]);
Expand All @@ -57,7 +62,6 @@ describe('configure', () => {

describe('middleware', () => {
it("runs middlewares' onStateChange when uiState changes", async () => {
const container = document.createElement('div');
const search = instantsearch({
indexName: 'instant_search',
searchClient: createSearchClient(),
Expand All @@ -74,25 +78,17 @@ describe('middleware', () => {

search.use(() => middlewareDefinition);

search.addWidgets([
searchBox({
container,
placeholder: 'search',
}),
]);
search.addWidgets([connectSearchBox(() => {})({})]);

search.start();

fireEvent.input(container.querySelector('input')!, {
target: { value: 'q' },
});
search.renderState.instant_search.searchBox!.refine('q');

await wait(0);
expect(middlewareDefinition.onStateChange).toHaveBeenCalledTimes(1);
});

it("runs middlewares' onStateChange when uiState changes with user-provided onStateChange param", async () => {
const container = document.createElement('div');
const search = instantsearch({
indexName: 'instant_search',
searchClient: createSearchClient(),
Expand All @@ -112,27 +108,18 @@ describe('middleware', () => {

search.use(() => middlewareDefinition);

search.addWidgets([
searchBox({
container,
placeholder: 'search',
}),
]);
search.addWidgets([connectSearchBox(() => {})({})]);

search.start();

fireEvent.input(container.querySelector('input')!, {
target: { value: 'q' },
});
search.renderState.instant_search.searchBox!.refine('q');

await wait(0);
expect(middlewareDefinition.onStateChange).toHaveBeenCalledTimes(1);
});
});

describe('errors', () => {
const virtualSearchBox = connectSearchBox(() => {});

it('client errors can be handled', () => {
const search = instantsearch({
searchClient: createSearchClient({
Expand All @@ -143,7 +130,7 @@ describe('errors', () => {
indexName: '123',
});

search.addWidgets([virtualSearchBox({})]);
search.addWidgets([connectSearchBox(() => {})({})]);

expect.assertions(4);

Expand Down Expand Up @@ -184,7 +171,7 @@ describe('network requests', () => {
indexName: 'indexName',
searchClient,
})
.addWidgets([searchBox({ container: document.createElement('div') })])
.addWidgets([connectSearchBox(() => {})({})])
.start();

await wait(0);
Expand Down Expand Up @@ -212,9 +199,8 @@ describe('network requests', () => {
searchClient,
})
.addWidgets([
frequentlyBoughtTogether({
connectFrequentlyBoughtTogether(() => {})({
objectIDs: ['one'],
container: document.createElement('div'),
}),
])
.start();
Expand Down Expand Up @@ -250,10 +236,9 @@ describe('network requests', () => {
searchClient,
})
.addWidgets([
searchBox({ container: document.createElement('div') }),
frequentlyBoughtTogether({
connectSearchBox(() => {})({}),
connectFrequentlyBoughtTogether(() => {})({
objectIDs: ['one'],
container: document.createElement('div'),
}),
])
.start();
Expand Down Expand Up @@ -327,7 +312,7 @@ describe('network requests', () => {
searchClient,
insights: true,
})
.addWidgets([searchBox({ container: document.createElement('div') })])
.addWidgets([connectSearchBox(() => {})({})])
.start();

await wait(0);
Expand Down Expand Up @@ -358,9 +343,8 @@ describe('network requests', () => {
insights: true,
})
.addWidgets([
frequentlyBoughtTogether({
connectFrequentlyBoughtTogether(() => {})({
objectIDs: ['one'],
container: document.createElement('div'),
}),
])
.start();
Expand Down Expand Up @@ -397,10 +381,9 @@ describe('network requests', () => {
insights: true,
})
.addWidgets([
searchBox({ container: document.createElement('div') }),
frequentlyBoughtTogether({
connectSearchBox(() => {})({}),
connectFrequentlyBoughtTogether(() => {})({
objectIDs: ['one'],
container: document.createElement('div'),
}),
])
.start();
Expand Down Expand Up @@ -476,7 +459,7 @@ describe('network requests', () => {
searchClient,
insights: true,
})
.addWidgets([searchBox({ container: document.createElement('div') })])
.addWidgets([connectSearchBox(() => {})({})])
.start();

await wait(0);
Expand Down Expand Up @@ -507,9 +490,8 @@ describe('network requests', () => {
insights: true,
})
.addWidgets([
frequentlyBoughtTogether({
connectFrequentlyBoughtTogether(() => {})({
objectIDs: ['one'],
container: document.createElement('div'),
}),
])
.start();
Expand Down Expand Up @@ -546,10 +528,9 @@ describe('network requests', () => {
insights: true,
})
.addWidgets([
searchBox({ container: document.createElement('div') }),
frequentlyBoughtTogether({
connectSearchBox(() => {})({}),
connectFrequentlyBoughtTogether(() => {})({
objectIDs: ['one'],
container: document.createElement('div'),
}),
])
.start();
Expand Down Expand Up @@ -592,12 +573,11 @@ describe('network requests', () => {
describe('interactive life cycle', () => {
it('sends no queries when widgets are removed', async () => {
const searchClient = createRecommendSearchClient();
const searchBoxWidget = searchBox({
container: document.createElement('div'),
});
const frequentlyBoughtTogetherWidget = frequentlyBoughtTogether({
const searchBoxWidget = connectSearchBox(() => {})({});
const frequentlyBoughtTogetherWidget = connectFrequentlyBoughtTogether(
() => {}
)({
objectIDs: ['one'],
container: document.createElement('div'),
});
const search = instantsearch({
indexName: 'indexName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ import { wait } from '@instantsearch/testutils/wait';
import originalHelper from 'algoliasearch-helper';
import { h, render, createRef } from 'preact';

import { createRenderOptions, createWidget } from '../../../test/createWidget';
import { connectSearchBox, connectPagination } from '../../connectors';
import { createInsightsMiddleware } from '../../middlewares';
import { index } from '../../widgets';
import InstantSearch from '../InstantSearch';
import {
InstantSearch,
index,
createInsightsMiddleware,
connectSearchBox,
connectPagination,
} from '..';
import { createRenderOptions, createWidget } from '../../test/createWidget';
import version from '../version';

import type {
UiState,
Widget,
IndexWidget,
PaginationConnectorParams,
PaginationWidgetDescription,
SearchBoxWidgetDescription,
SearchBoxConnectorParams,
} from '../../types';
} from '..';
import type { UiState, Widget, IndexWidget } from '../types';
import type { RefObject } from 'preact';

type SearchBoxWidgetInstance = Widget<
Expand Down Expand Up @@ -318,12 +319,9 @@ describe('InstantSearch', () => {
searchClient,
});

expect(searchClient.addAlgoliaAgent).toHaveBeenCalledTimes(2);
expect(searchClient.addAlgoliaAgent).toHaveBeenCalledWith(
expect.stringMatching(/^instantsearch-core \(.*\)$/)
);
expect(searchClient.addAlgoliaAgent).toHaveBeenCalledTimes(1);
expect(searchClient.addAlgoliaAgent).toHaveBeenCalledWith(
`instantsearch.js (${version})`
`instantsearch-core (${version})`
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
} from '@instantsearch/mocks';

import {
getInitialResults,
waitForResults,
instantsearch,
index,
connectConfigure,
connectHierarchicalMenu,
connectSearchBox,
} from '../../connectors';
import instantsearch from '../../index.es';
import { index } from '../../widgets';
import { getInitialResults, waitForResults } from '../server';
} from '..';

describe('waitForResults', () => {
test('waits for the results from the search instance', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';

import { connectSearchBox } from '../../connectors';
import instantsearch from '../../index.es';
import { connectSearchBox } from '../connectors';
import { instantsearch } from '../instantsearch';

import type InstantSearch from '../InstantSearch';
import type { InstantSearch } from '../instantsearch';

function createDelayedSearchClient(timeout: number) {
const searchClient = createSearchClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';

import { connectPagination, connectSearchBox } from '../../../connectors';
import instantsearch from '../../../index.es';
import { index } from '../../../widgets';
import historyRouter from '../../routers/history';
import {
instantsearch,
index,
historyRouter,
connectPagination,
connectSearchBox,
} from '../..';

beforeEach(() => {
window.history.pushState({}, '', '/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';

import instantsearch from '../../..';
import { connectSearchBox } from '../../../connectors';
import historyRouter from '../../routers/history';
import { instantsearch, historyRouter, connectSearchBox } from '../..';

import type InstantSearch from '../../InstantSearch';
import type { InstantSearch } from '../..';

/* eslint no-lone-blocks: "off" */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';

import instantsearch from '../../..';
import { connectPagination, connectSearchBox } from '../../../connectors';
import historyRouter from '../../routers/history';
import {
instantsearch,
historyRouter,
connectPagination,
connectSearchBox,
} from '../..';

/* eslint no-lone-blocks: "off" */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';

import instantsearch from '../../..';
import { connectSearchBox } from '../../../connectors';
import historyRouter from '../../routers/history';
import { instantsearch, historyRouter, connectSearchBox } from '../..';

/* eslint no-lone-blocks: "off" */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils/wait';

import instantsearch from '../../..';
import { connectSearchBox } from '../../../connectors';
import historyRouter from '../../routers/history';
import { instantsearch, historyRouter, connectSearchBox } from '../..';

/* eslint no-lone-blocks: "off" */

Expand Down
Loading

0 comments on commit 4f82be2

Please sign in to comment.