-
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
93ca5f0
commit 11cf12e
Showing
6 changed files
with
885 additions
and
159 deletions.
There are no files selected for viewing
223 changes: 223 additions & 0 deletions
223
packages/recommend-js/src/__tests__/frequentlyBoughtTogether.test.tsx
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,223 @@ | ||
/** @jsx h */ | ||
import { ObjectWithObjectID } from '@algolia/client-search'; | ||
import { waitFor, within } from '@testing-library/dom'; | ||
import { h } from 'preact'; | ||
|
||
import { createMultiSearchResponse } from '../../../../test/utils/createApiResponse'; | ||
import { | ||
createRecommendClient, | ||
hit, | ||
} from '../../../../test/utils/createRecommendClient'; | ||
import { frequentlyBoughtTogether } from '../frequentlyBoughtTogether'; | ||
|
||
function createMockedRecommendClient(recommendations: ObjectWithObjectID[]) { | ||
const recommendClient = createRecommendClient({ | ||
getFrequentlyBoughtTogether: jest.fn(() => | ||
Promise.resolve( | ||
createMultiSearchResponse({ | ||
hits: recommendations, | ||
}) | ||
) | ||
), | ||
}); | ||
|
||
return { | ||
recommendClient, | ||
}; | ||
} | ||
|
||
describe('frequently bought together', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
describe('Rendering for header and item', () => { | ||
test('renders JSX templates', async () => { | ||
const panelContainer = document.createElement('div'); | ||
panelContainer.setAttribute('id', 'frequentlyBoughtTogether'); | ||
|
||
const { recommendClient } = createMockedRecommendClient( | ||
hit.recommendations | ||
); | ||
|
||
document.body.appendChild(panelContainer); | ||
|
||
frequentlyBoughtTogether<ObjectWithObjectID>({ | ||
container: '#frequentlyBoughtTogether', | ||
recommendClient, | ||
indexName: 'products', | ||
objectIDs: ['D06270-9132-995'], | ||
headerComponent: () => <h1>FBT</h1>, | ||
itemComponent: ({ item }) => <div>{item.objectID}</div>, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(within(panelContainer).getAllByRole('listitem')).not.toBeNull(); | ||
expect(panelContainer).toMatchInlineSnapshot(` | ||
<div | ||
id="frequentlyBoughtTogether" | ||
> | ||
<section | ||
class="auc-Recommend" | ||
> | ||
<h1> | ||
FBT | ||
</h1> | ||
<div | ||
class="auc-Recommend-container" | ||
> | ||
<ol | ||
class="auc-Recommend-list" | ||
> | ||
<li | ||
class="auc-Recommend-item" | ||
> | ||
<div> | ||
1 | ||
</div> | ||
</li> | ||
<li | ||
class="auc-Recommend-item" | ||
> | ||
<div> | ||
2 | ||
</div> | ||
</li> | ||
<li | ||
class="auc-Recommend-item" | ||
> | ||
<div> | ||
3 | ||
</div> | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
test('renders templates using createElement and Fragment', async () => { | ||
const panelContainer = document.createElement('div'); | ||
panelContainer.setAttribute('id', 'frequentlyBoughtTogether'); | ||
|
||
const { recommendClient } = createMockedRecommendClient( | ||
hit.recommendations | ||
); | ||
|
||
document.body.appendChild(panelContainer); | ||
|
||
frequentlyBoughtTogether<ObjectWithObjectID>({ | ||
container: '#frequentlyBoughtTogether', | ||
recommendClient, | ||
indexName: 'products', | ||
objectIDs: ['D06270-9132-995'], | ||
headerComponent: ({ createElement }) => | ||
createElement('h1', null, 'FBT'), | ||
itemComponent: ({ item, createElement, Fragment }) => | ||
createElement(Fragment, null, item.objectID), | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(within(panelContainer).getAllByRole('listitem')).not.toBeNull(); | ||
expect(panelContainer).toMatchInlineSnapshot(` | ||
<div | ||
id="frequentlyBoughtTogether" | ||
> | ||
<section | ||
class="auc-Recommend" | ||
> | ||
<h1> | ||
FBT | ||
</h1> | ||
<div | ||
class="auc-Recommend-container" | ||
> | ||
<ol | ||
class="auc-Recommend-list" | ||
> | ||
<li | ||
class="auc-Recommend-item" | ||
> | ||
1 | ||
</li> | ||
<li | ||
class="auc-Recommend-item" | ||
> | ||
2 | ||
</li> | ||
<li | ||
class="auc-Recommend-item" | ||
> | ||
3 | ||
</li> | ||
</ol> | ||
</div> | ||
</section> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Rendering fallbackComponent', () => { | ||
test('renders JSX templates', async () => { | ||
const panelContainer = document.createElement('div'); | ||
panelContainer.setAttribute('id', 'frequentlyBoughtTogether'); | ||
|
||
const { recommendClient } = createMockedRecommendClient([]); | ||
|
||
document.body.appendChild(panelContainer); | ||
|
||
frequentlyBoughtTogether<ObjectWithObjectID>({ | ||
container: '#frequentlyBoughtTogether', | ||
recommendClient, | ||
indexName: 'products', | ||
objectIDs: ['D06270-9132-995'], | ||
itemComponent: ({ item }) => <div>{item.objectID}</div>, | ||
fallbackComponent: () => <div>Fallback component</div>, | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(within(panelContainer).getByText('Fallback component')) | ||
.toMatchInlineSnapshot(` | ||
<div> | ||
Fallback component | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
test('renders templates using createElement and Fragment', async () => { | ||
const panelContainer = document.createElement('div'); | ||
panelContainer.setAttribute('id', 'frequentlyBoughtTogether'); | ||
|
||
const { recommendClient } = createMockedRecommendClient([]); | ||
|
||
document.body.appendChild(panelContainer); | ||
|
||
frequentlyBoughtTogether<ObjectWithObjectID>({ | ||
container: '#frequentlyBoughtTogether', | ||
recommendClient, | ||
indexName: 'products', | ||
objectIDs: ['D06270-9132-995'], | ||
itemComponent: ({ item, createElement }) => | ||
createElement('div', null, item.objectID), | ||
fallbackComponent: ({ createElement, Fragment }) => | ||
createElement(Fragment, null, 'Fallback component'), | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(within(panelContainer).getByText('Fallback component')) | ||
.toMatchInlineSnapshot(` | ||
<div | ||
id="frequentlyBoughtTogether" | ||
> | ||
Fallback component | ||
</div> | ||
`); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.