Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rendering): Expose createElement #85

Merged
merged 25 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b9d8236
feat: return createElement and Fragment
SophieManley03 Jun 8, 2022
cf0254c
feat: return createElement and Fragment for fallback
SophieManley03 Jun 8, 2022
bcfa781
feat: use Renderer
SophieManley03 Jun 13, 2022
ff0c5f9
feat: add tests
SophieManley03 Jun 13, 2022
3d9f32d
Update packages/recommend-vdom/src/types/FacetsViewProps.ts
SophieManley03 Jun 13, 2022
ef91a7c
Update packages/recommend-vdom/src/types/ViewProps.ts
SophieManley03 Jun 13, 2022
313d400
fix: missing import
SophieManley03 Jun 13, 2022
9aa3739
fix: typings
SophieManley03 Jun 13, 2022
6e822f0
fix: lint
SophieManley03 Jun 13, 2022
93ca5f0
fix: size limit
SophieManley03 Jun 13, 2022
11cf12e
fix: add more tests
SophieManley03 Jun 14, 2022
b5ff0d9
fix: remove useless if
SophieManley03 Jun 17, 2022
c84b6ec
Update packages/recommend-js/src/__tests__/frequentlyBoughtTogether.t…
SophieManley03 Jun 21, 2022
6b8a8f4
Update packages/recommend-js/src/__tests__/frequentlyBoughtTogether.t…
SophieManley03 Jun 21, 2022
d05c46b
Update packages/recommend-js/src/__tests__/frequentlyBoughtTogether.t…
SophieManley03 Jun 21, 2022
4e0509f
Update packages/recommend-js/src/__tests__/frequentlyBoughtTogether.t…
SophieManley03 Jun 21, 2022
6cfb5e7
Update packages/recommend-js/src/__tests__/frequentlyBoughtTogether.t…
SophieManley03 Jun 21, 2022
52b64ad
Update packages/recommend-js/src/__tests__/trendingItems.test.tsx
SophieManley03 Jun 21, 2022
6bd6da8
Update packages/recommend-js/src/__tests__/trendingItems.test.tsx
SophieManley03 Jun 21, 2022
c19b3ba
Update packages/recommend-js/src/__tests__/trendingItems.test.tsx
SophieManley03 Jun 21, 2022
fbd7648
Update packages/recommend-js/src/__tests__/trendingItems.test.tsx
SophieManley03 Jun 21, 2022
a0bc683
Update packages/recommend-js/src/__tests__/trendingItems.test.tsx
SophieManley03 Jun 21, 2022
c53d2c4
Update packages/recommend-js/src/__tests__/relatedProducts.test.tsx
SophieManley03 Jun 21, 2022
6674f98
Update packages/recommend-js/src/__tests__/relatedProducts.test.tsx
SophieManley03 Jun 21, 2022
be21c6d
fix: naming in test
SophieManley03 Jun 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"path": "packages/recommend-js/dist/umd/index.js",
"maxSize": "7.30 kB"
"maxSize": "7.35 kB"
},
{
"path": "packages/recommend-react/dist/umd/index.js",
Expand Down
207 changes: 207 additions & 0 deletions packages/recommend-js/src/__tests__/frequentlyBoughtTogether.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/** @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('frequentlyBoughtTogether', () => {
beforeEach(() => {
document.body.innerHTML = '';
});

describe('rendering the header and items', () => {
test('renders JSX templates', async () => {
const container = document.createElement('div');

const recommendClient = createMockedRecommendClient(hit.recommendations);

document.body.appendChild(container);

frequentlyBoughtTogether<ObjectWithObjectID>({
container,
recommendClient,
indexName: 'products',
objectIDs: ['D06270-9132-995'],
headerComponent: () => <h1>FBT</h1>,
itemComponent: ({ item }) => <div>{item.objectID}</div>,
});

await waitFor(() => {
expect(within(container).getAllByRole('listitem')).not.toBeNull();
expect(container).toMatchInlineSnapshot(`
<div>
<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 using `createElement` and `Fragment`', async () => {
const container = document.createElement('div');

const recommendClient = createMockedRecommendClient(hit.recommendations);

document.body.appendChild(container);

frequentlyBoughtTogether<ObjectWithObjectID>({
container,
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(container).getAllByRole('listitem')).not.toBeNull();
expect(container).toMatchInlineSnapshot(`
<div>
<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 container = document.createElement('div');

const recommendClient = createMockedRecommendClient([]);

document.body.appendChild(container);

frequentlyBoughtTogether<ObjectWithObjectID>({
container,
recommendClient,
indexName: 'products',
objectIDs: ['D06270-9132-995'],
itemComponent: ({ item }) => <div>{item.objectID}</div>,
fallbackComponent: () => <div>Fallback component</div>,
});

await waitFor(() => {
expect(within(container).getByText('Fallback component'))
.toMatchInlineSnapshot(`
<div>
Fallback component
</div>
`);
});
});

test('renders using `createElement` and `Fragment`', async () => {
const container = document.createElement('div');

const recommendClient = createMockedRecommendClient([]);

document.body.appendChild(container);

frequentlyBoughtTogether<ObjectWithObjectID>({
container,
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(container).getByText('Fallback component'))
.toMatchInlineSnapshot(`
<div>
Fallback component
</div>
`);
});
});
});
});
Loading