This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <hello@haroen.me>
- Loading branch information
Showing
9 changed files
with
212 additions
and
25 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
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,51 @@ | ||
<template> | ||
<div | ||
v-if="state && state.isVirtualReplica" | ||
:class="suit()" | ||
> | ||
<slot | ||
:is-relevant-sorted="state.isRelevantSorted" | ||
:refine="state.refine" | ||
> | ||
<div :class="suit('text')"> | ||
<slot | ||
name="text" | ||
:is-relevant-sorted="state.isRelevantSorted" | ||
/> | ||
</div> | ||
<button | ||
type="button" | ||
:class="suit('button')" | ||
@click="refine()" | ||
> | ||
<slot | ||
name="button" | ||
:is-relevant-sorted="state.isRelevantSorted" | ||
>{{ state.isRelevantSorted ? 'See all results' : 'See relevant results' }}</slot> | ||
</button> | ||
</slot> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { connectRelevantSort } from 'instantsearch.js/es/connectors'; | ||
import { createWidgetMixin } from '../mixins/widget'; | ||
import { createSuitMixin } from '../mixins/suit'; | ||
export default { | ||
name: 'AisRelevantSort', | ||
mixins: [ | ||
createSuitMixin({ name: 'RelevantSort' }), | ||
createWidgetMixin({ connector: connectRelevantSort }), | ||
], | ||
methods: { | ||
refine() { | ||
if (this.state.isRelevantSorted) { | ||
this.state.refine(0); | ||
} else { | ||
this.state.refine(undefined); | ||
} | ||
}, | ||
}, | ||
}; | ||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import RelevantSort from '../RelevantSort.vue'; | ||
import { __setState } from '../../mixins/widget'; | ||
jest.mock('../../mixins/widget'); | ||
|
||
describe('renders correctly', () => { | ||
test('no virtual replica', () => { | ||
__setState({ | ||
isVirtualReplica: false, | ||
isRelevantSorted: false, | ||
}); | ||
const wrapper = mount(RelevantSort); | ||
expect(wrapper.html()).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
test('not relevant sorted', () => { | ||
__setState({ | ||
isVirtualReplica: true, | ||
isRelevantSorted: false, | ||
}); | ||
const wrapper = mount(RelevantSort); | ||
expect(wrapper.html()).toMatchInlineSnapshot(` | ||
<div class="ais-RelevantSort"> | ||
<div class="ais-RelevantSort-text"> | ||
</div> | ||
<button type="button" | ||
class="ais-RelevantSort-button" | ||
> | ||
See relevant results | ||
</button> | ||
</div> | ||
`); | ||
}); | ||
|
||
test('relevant sorted', () => { | ||
__setState({ | ||
isVirtualReplica: true, | ||
isRelevantSorted: true, | ||
}); | ||
const wrapper = mount(RelevantSort); | ||
expect(wrapper.html()).toMatchInlineSnapshot(` | ||
<div class="ais-RelevantSort"> | ||
<div class="ais-RelevantSort-text"> | ||
</div> | ||
<button type="button" | ||
class="ais-RelevantSort-button" | ||
> | ||
See all results | ||
</button> | ||
</div> | ||
`); | ||
}); | ||
}); | ||
|
||
it("calls the connector's refine function with 0 and undefined", () => { | ||
__setState({ | ||
isRelevantSorted: true, | ||
isVirtualReplica: true, | ||
refine: jest.fn(() => { | ||
wrapper.vm.state.isRelevantSorted = !wrapper.vm.state.isRelevantSorted; | ||
}), | ||
}); | ||
|
||
const wrapper = mount(RelevantSort); | ||
|
||
const button = wrapper.find('button'); | ||
|
||
button.trigger('click'); | ||
expect(wrapper.vm.state.refine).toHaveBeenLastCalledWith(0); | ||
|
||
button.trigger('click'); | ||
expect(wrapper.vm.state.refine).toHaveBeenLastCalledWith(undefined); | ||
|
||
button.trigger('click'); | ||
expect(wrapper.vm.state.refine).toHaveBeenLastCalledWith(0); | ||
}); |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import algoliasearch from 'algoliasearch/lite'; | ||
import { storiesOf } from '@storybook/vue'; | ||
import { previewWrapper } from './utils'; | ||
|
||
storiesOf('ais-relevant-sort', module) | ||
.addDecorator( | ||
previewWrapper({ | ||
searchClient: algoliasearch( | ||
'C7RIRJRYR9', | ||
'77af6d5ffb27caa5ff4937099fcb92e8' | ||
), | ||
indexName: 'test_Bestbuy_vr_price_asc', | ||
}) | ||
) | ||
.add('default', () => ({ | ||
template: '<ais-relevant-sort></ais-relevant-sort>', | ||
})) | ||
.add('with custom text', () => ({ | ||
template: ` | ||
<ais-relevant-sort> | ||
<template slot="text" slot-scope="{ isRelevantSorted }"> | ||
{{ isRelevantSorted | ||
? 'We removed some search results to show you the most relevant ones' | ||
: 'Currently showing all results' }} | ||
</template> | ||
</ais-relevant-sort> | ||
`, | ||
})); |
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