Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 4fe5745

Browse files
Eunjae LeeHaroenv
andauthored
feat(ais-relevant-sort): add widget (#918)
* feat(smartSort): add widget * add story * add isSmartSorted to text slot * fix story * fix(smartsort): don't render if not virtual replica * test(smartsort): add tests * chore(deps): update instantsearch * chore(deps): update * chore: update bundlesize * update to relevantSort * feat(stats): apply relevant sort * chore: update instantsearch Co-authored-by: Haroen Viaene <[email protected]>
1 parent 3c3f7e2 commit 4fe5745

File tree

9 files changed

+212
-25
lines changed

9 files changed

+212
-25
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"dependencies": {
4848
"algoliasearch-helper": "^3.1.0",
49-
"instantsearch.js": "^4.11.0"
49+
"instantsearch.js": "^4.16.1"
5050
},
5151
"peerDependencies": {
5252
"algoliasearch": ">= 3.32.0 < 5",
@@ -114,11 +114,11 @@
114114
"bundlesize": [
115115
{
116116
"path": "./dist/vue-instantsearch.js",
117-
"maxSize": "48.10 kB"
117+
"maxSize": "52.50 kB"
118118
},
119119
{
120120
"path": "./dist/vue-instantsearch.common.js",
121-
"maxSize": "16 kB"
121+
"maxSize": "16.25 kB"
122122
}
123123
],
124124
"resolutions": {

src/components/RelevantSort.vue

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<template>
2+
<div
3+
v-if="state && state.isVirtualReplica"
4+
:class="suit()"
5+
>
6+
<slot
7+
:is-relevant-sorted="state.isRelevantSorted"
8+
:refine="state.refine"
9+
>
10+
<div :class="suit('text')">
11+
<slot
12+
name="text"
13+
:is-relevant-sorted="state.isRelevantSorted"
14+
/>
15+
</div>
16+
<button
17+
type="button"
18+
:class="suit('button')"
19+
@click="refine()"
20+
>
21+
<slot
22+
name="button"
23+
:is-relevant-sorted="state.isRelevantSorted"
24+
>{{ state.isRelevantSorted ? 'See all results' : 'See relevant results' }}</slot>
25+
</button>
26+
</slot>
27+
</div>
28+
</template>
29+
30+
<script>
31+
import { connectRelevantSort } from 'instantsearch.js/es/connectors';
32+
import { createWidgetMixin } from '../mixins/widget';
33+
import { createSuitMixin } from '../mixins/suit';
34+
35+
export default {
36+
name: 'AisRelevantSort',
37+
mixins: [
38+
createSuitMixin({ name: 'RelevantSort' }),
39+
createWidgetMixin({ connector: connectRelevantSort }),
40+
],
41+
methods: {
42+
refine() {
43+
if (this.state.isRelevantSorted) {
44+
this.state.refine(0);
45+
} else {
46+
this.state.refine(undefined);
47+
}
48+
},
49+
},
50+
};
51+
</script>

src/components/Stats.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
v-bind="state"
88
:results="state.instantSearchInstance.helper.lastResults"
99
>
10-
<span :class="suit('text')">{{ state.nbHits.toLocaleString() }} results found in {{ state.processingTimeMS.toLocaleString() }}ms</span>
10+
<span :class="suit('text')"><template v-if="state.areHitsSorted">{{ state.nbSortedHits.toLocaleString() }} relevant results sorted out of {{ state.nbHits.toLocaleString() }}</template><template v-else>{{ state.nbHits.toLocaleString() }} results</template> found in {{ state.processingTimeMS.toLocaleString() }}ms</span>
1111
</slot>
1212
</div>
1313
</template>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { mount } from '@vue/test-utils';
2+
import RelevantSort from '../RelevantSort.vue';
3+
import { __setState } from '../../mixins/widget';
4+
jest.mock('../../mixins/widget');
5+
6+
describe('renders correctly', () => {
7+
test('no virtual replica', () => {
8+
__setState({
9+
isVirtualReplica: false,
10+
isRelevantSorted: false,
11+
});
12+
const wrapper = mount(RelevantSort);
13+
expect(wrapper.html()).toMatchInlineSnapshot(`undefined`);
14+
});
15+
16+
test('not relevant sorted', () => {
17+
__setState({
18+
isVirtualReplica: true,
19+
isRelevantSorted: false,
20+
});
21+
const wrapper = mount(RelevantSort);
22+
expect(wrapper.html()).toMatchInlineSnapshot(`
23+
24+
<div class="ais-RelevantSort">
25+
<div class="ais-RelevantSort-text">
26+
</div>
27+
<button type="button"
28+
class="ais-RelevantSort-button"
29+
>
30+
See relevant results
31+
</button>
32+
</div>
33+
34+
`);
35+
});
36+
37+
test('relevant sorted', () => {
38+
__setState({
39+
isVirtualReplica: true,
40+
isRelevantSorted: true,
41+
});
42+
const wrapper = mount(RelevantSort);
43+
expect(wrapper.html()).toMatchInlineSnapshot(`
44+
45+
<div class="ais-RelevantSort">
46+
<div class="ais-RelevantSort-text">
47+
</div>
48+
<button type="button"
49+
class="ais-RelevantSort-button"
50+
>
51+
See all results
52+
</button>
53+
</div>
54+
55+
`);
56+
});
57+
});
58+
59+
it("calls the connector's refine function with 0 and undefined", () => {
60+
__setState({
61+
isRelevantSorted: true,
62+
isVirtualReplica: true,
63+
refine: jest.fn(() => {
64+
wrapper.vm.state.isRelevantSorted = !wrapper.vm.state.isRelevantSorted;
65+
}),
66+
});
67+
68+
const wrapper = mount(RelevantSort);
69+
70+
const button = wrapper.find('button');
71+
72+
button.trigger('click');
73+
expect(wrapper.vm.state.refine).toHaveBeenLastCalledWith(0);
74+
75+
button.trigger('click');
76+
expect(wrapper.vm.state.refine).toHaveBeenLastCalledWith(undefined);
77+
78+
button.trigger('click');
79+
expect(wrapper.vm.state.refine).toHaveBeenLastCalledWith(0);
80+
});

src/components/__tests__/Stats.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Stats from '../Stats.vue';
44

55
import { __setState } from '../../mixins/widget';
66
jest.mock('../../mixins/widget');
7+
78
it('renders correctly', () => {
89
__setState({
910
hitsPerPage: 50,
@@ -20,5 +21,42 @@ it('renders correctly', () => {
2021
});
2122

2223
const wrapper = mount(Stats);
23-
expect(wrapper.html()).toMatchSnapshot();
24+
expect(wrapper.html()).toMatchInlineSnapshot(`
25+
26+
<div class="ais-Stats">
27+
<span class="ais-Stats-text">
28+
1,000 results found in 12ms
29+
</span>
30+
</div>
31+
32+
`);
33+
});
34+
35+
it('renders correctly (relevant sort)', () => {
36+
__setState({
37+
areHitsSorted: true,
38+
hitsPerPage: 50,
39+
nbPages: 20,
40+
nbHits: 1000,
41+
nbSortedHits: 12,
42+
page: 2,
43+
processingTimeMS: 12,
44+
query: 'ipho',
45+
instantSearchInstance: {
46+
helper: {
47+
lastResults: [],
48+
},
49+
},
50+
});
51+
52+
const wrapper = mount(Stats);
53+
expect(wrapper.html()).toMatchInlineSnapshot(`
54+
55+
<div class="ais-Stats">
56+
<span class="ais-Stats-text">
57+
12 relevant results sorted out of 1,000 found in 12ms
58+
</span>
59+
</div>
60+
61+
`);
2462
});

src/components/__tests__/__snapshots__/Stats.js.snap

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/widgets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ export {
4242
default as AisToggleRefinement,
4343
} from './components/ToggleRefinement.vue';
4444
export { default as AisVoiceSearch } from './components/VoiceSearch.vue';
45+
export { default as AisRelevantSort } from './components/RelevantSort.vue';

stories/RelevantSort.stories.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import algoliasearch from 'algoliasearch/lite';
2+
import { storiesOf } from '@storybook/vue';
3+
import { previewWrapper } from './utils';
4+
5+
storiesOf('ais-relevant-sort', module)
6+
.addDecorator(
7+
previewWrapper({
8+
searchClient: algoliasearch(
9+
'C7RIRJRYR9',
10+
'77af6d5ffb27caa5ff4937099fcb92e8'
11+
),
12+
indexName: 'test_Bestbuy_vr_price_asc',
13+
})
14+
)
15+
.add('default', () => ({
16+
template: '<ais-relevant-sort></ais-relevant-sort>',
17+
}))
18+
.add('with custom text', () => ({
19+
template: `
20+
<ais-relevant-sort>
21+
<template slot="text" slot-scope="{ isRelevantSorted }">
22+
{{ isRelevantSorted
23+
? 'We removed some search results to show you the most relevant ones'
24+
: 'Currently showing all results' }}
25+
</template>
26+
</ais-relevant-sort>
27+
`,
28+
}));

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,10 +1156,10 @@ algoliasearch-helper@^3.1.0:
11561156
dependencies:
11571157
events "^1.1.1"
11581158

1159-
algoliasearch-helper@^3.3.4:
1160-
version "3.3.4"
1161-
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.3.4.tgz#4a3c56d42a2a81589d5722b73653b2deaf3e7064"
1162-
integrity sha512-1Ts2XcgGdjGlDrp3v6zbY8VW+X9+jJ5rBmtPBmXOQLd4b5t/LpJlaBdxoAnlMfVFjywP7KSAdmyFUNNYVHDyRQ==
1159+
algoliasearch-helper@^3.4.4:
1160+
version "3.4.4"
1161+
resolved "https://registry.yarnpkg.com/algoliasearch-helper/-/algoliasearch-helper-3.4.4.tgz#f2eb46bc4d2f6fed82c7201b8ac4ce0a1988ae67"
1162+
integrity sha512-OjyVLjykaYKCMxxRMZNiwLp8CS310E0qAeIY2NaublcmLAh8/SL19+zYHp7XCLtMem2ZXwl3ywMiA32O9jszuw==
11631163
dependencies:
11641164
events "^1.1.1"
11651165

@@ -7892,13 +7892,13 @@ [email protected]:
78927892
resolved "https://registry.yarnpkg.com/instantsearch.css/-/instantsearch.css-7.3.1.tgz#7ab74a8f355091ae040947a9cf5438f379026622"
78937893
integrity sha512-/kaMDna5D+Q9mImNBHEhb9HgHATDOFKYii7N1Iwvrj+lmD9gBJLqVhUw67gftq2O0QI330pFza+CRscIwB1wQQ==
78947894

7895-
instantsearch.js@^4.11.0:
7896-
version "4.11.0"
7897-
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.11.0.tgz#9e55426fbcf1929df9a772c53eeae282575e1a13"
7898-
integrity sha512-SVdS63S7skry5ebttPOq9D/BB2H61lYL2qdRiGdeuxsbrkwNssvpmtt/kAKxrnySUNxiSI2ZKTH2wa2WaMU98A==
7895+
instantsearch.js@^4.16.1:
7896+
version "4.16.1"
7897+
resolved "https://registry.yarnpkg.com/instantsearch.js/-/instantsearch.js-4.16.1.tgz#78e00d2256c89693a94f58ebfc5f0864953b7ec8"
7898+
integrity sha512-NfwNOb+Ftj7Y+h6lW7iCd5SXWKIHQZ981ldSddEHbgTexbdJEyxgWhaF8c3HajCySp8wZPD2gj9KpNQy/BsUgQ==
78997899
dependencies:
79007900
"@types/googlemaps" "^3.39.6"
7901-
algoliasearch-helper "^3.3.4"
7901+
algoliasearch-helper "^3.4.4"
79027902
classnames "^2.2.5"
79037903
events "^1.1.0"
79047904
hogan.js "^3.0.2"

0 commit comments

Comments
 (0)