-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf0254c
commit 3f8ded1
Showing
4 changed files
with
236 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
/** @jsx h */ | ||
//import { frequentlyBoughtTogether } from '@algolia/recommend-js'; | ||
import { fireEvent, waitFor, within } from '@testing-library/dom'; | ||
import { createMultiSearchResponse } from '../../../../test/utils/createApiResponse'; | ||
import { | ||
createRecommendClient, | ||
hit, | ||
} from '../../../../test/utils/createRecommendClient'; | ||
import { frequentlyBoughtTogether } from '../../dist/esm'; | ||
|
||
function createMockedRecommendClient() { | ||
const recommendClient = createRecommendClient({ | ||
getFrequentlyBoughtTogether: jest.fn(() => | ||
Promise.resolve( | ||
createMultiSearchResponse({ | ||
hits: [hit], | ||
}) | ||
) | ||
), | ||
}); | ||
|
||
return { | ||
recommendClient, | ||
}; | ||
} | ||
|
||
describe('templates', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
test('renders JSX templates', async () => { | ||
const panelContainer = document.createElement('div'); | ||
panelContainer.setAttribute('id', 'frequentlyBoughtTogether'); | ||
|
||
const { recommendClient } = createMockedRecommendClient(); | ||
|
||
document.body.appendChild(panelContainer); | ||
|
||
frequentlyBoughtTogether<any>({ | ||
container: '#frequentlyBoughtTogether', | ||
recommendClient, | ||
indexName: 'products', | ||
objectIDs: ['D06270-9132-995'], | ||
itemComponent: ({ item }) => <div>{item.objectID}</div>, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(within(panelContainer).getByText('1').parentNode) | ||
.toMatchInlineSnapshot(` | ||
<ul | ||
aria-labelledby="autocomplete-0-label" | ||
class="aa-List" | ||
id="autocomplete-0-list" | ||
role="listbox" | ||
> | ||
<li | ||
aria-selected="false" | ||
class="aa-Item" | ||
id="autocomplete-0-item-0" | ||
role="option" | ||
> | ||
<div | ||
class="MyCustomItemClass" | ||
> | ||
1 | ||
</div> | ||
</li> | ||
</ul> | ||
`); | ||
}); | ||
|
||
// fireEvent.input(input, { target: { value: 'nothing' } }); | ||
|
||
// await waitFor(() => { | ||
// expect(within(panelContainer).getByText('No results').parentNode) | ||
// .toMatchInlineSnapshot(` | ||
// <div | ||
// class="aa-SourceNoResults" | ||
// > | ||
// <div | ||
// class="MyCustomNoResultsClass" | ||
// > | ||
// No results | ||
// </div> | ||
// </div> | ||
// `); | ||
// }); | ||
}); | ||
|
||
// test('renders templates using `createElement`', async () => { | ||
// const container = document.createElement('div'); | ||
// const panelContainer = document.createElement('div'); | ||
|
||
// document.body.appendChild(panelContainer); | ||
|
||
// autocomplete<{ label: string }>({ | ||
// container, | ||
// panelContainer, | ||
// id: 'autocomplete-0', | ||
// getSources() { | ||
// return [ | ||
// { | ||
// sourceId: 'testSource', | ||
// getItems({ query }) { | ||
// return [{ label: '1' }].filter(({ label }) => | ||
// label.includes(query) | ||
// ); | ||
// }, | ||
// templates: { | ||
// header({ createElement }) { | ||
// return createElement( | ||
// 'header', | ||
// { className: 'MyCustomHeaderClass' }, | ||
// 'Header' | ||
// ); | ||
// }, | ||
// item({ item, createElement }) { | ||
// return createElement( | ||
// 'div', | ||
// { className: 'MyCustomItemClass' }, | ||
// item.label | ||
// ); | ||
// }, | ||
// footer({ createElement }) { | ||
// return createElement( | ||
// 'footer', | ||
// { className: 'MyCustomFooterClass' }, | ||
// 'Footer' | ||
// ); | ||
// }, | ||
// noResults({ createElement }) { | ||
// return createElement( | ||
// 'div', | ||
// { className: 'MyCustomNoResultsClass' }, | ||
// 'No results' | ||
// ); | ||
// }, | ||
// }, | ||
// }, | ||
// ]; | ||
// }, | ||
// }); | ||
|
||
// const input = container.querySelector<HTMLInputElement>('.aa-Input'); | ||
|
||
// fireEvent.input(input, { target: { value: '1' } }); | ||
|
||
// await waitFor(() => { | ||
// expect(within(panelContainer).getByRole('banner')).toMatchInlineSnapshot(` | ||
// <header | ||
// class="MyCustomHeaderClass" | ||
// > | ||
// Header | ||
// </header> | ||
// `); | ||
// expect(within(panelContainer).getByRole('listbox')) | ||
// .toMatchInlineSnapshot(` | ||
// <ul | ||
// aria-labelledby="autocomplete-0-label" | ||
// class="aa-List" | ||
// id="autocomplete-0-list" | ||
// role="listbox" | ||
// > | ||
// <li | ||
// aria-selected="false" | ||
// class="aa-Item" | ||
// id="autocomplete-0-item-0" | ||
// role="option" | ||
// > | ||
// <div | ||
// class="MyCustomItemClass" | ||
// > | ||
// 1 | ||
// </div> | ||
// </li> | ||
// </ul> | ||
// `); | ||
// expect(within(panelContainer).getByRole('contentinfo')) | ||
// .toMatchInlineSnapshot(` | ||
// <footer | ||
// class="MyCustomFooterClass" | ||
// > | ||
// Footer | ||
// </footer> | ||
// `); | ||
// }); | ||
|
||
// fireEvent.input(input, { target: { value: 'nothing' } }); | ||
|
||
// await waitFor(() => { | ||
// expect(within(panelContainer).getByText('No results').parentNode) | ||
// .toMatchInlineSnapshot(` | ||
// <div | ||
// class="aa-SourceNoResults" | ||
// > | ||
// <div | ||
// class="MyCustomNoResultsClass" | ||
// > | ||
// No results | ||
// </div> | ||
// </div> | ||
// `); | ||
// }); | ||
// }); | ||
}); | ||
|
||
function htmlShim(template: Array<string | any> | string, html: HTMLTemplate) { | ||
if (typeof template === 'string') { | ||
return html(([template] as unknown) as TemplateStringsArray); | ||
} | ||
|
||
const [strings, variables] = template.reduce( | ||
(acc, part, index) => { | ||
const isEven = index % 2 === 0; | ||
|
||
acc[Math.abs(Number(!isEven))].push(part); | ||
|
||
return acc; | ||
}, | ||
[[], []] | ||
); | ||
|
||
// Before TypeScript 2, `TemplateStringsArray` was assignable to `string[]`. | ||
// This is no longer the case, but `htm` does accept `string[]`. | ||
// Since this solution is for IE11 users who don't have a build step, it isn't | ||
// necessary to complexify the function to make it type-safe. | ||
return html((strings as unknown) as TemplateStringsArray, ...variables); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters