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

Commit 56917a0

Browse files
authored
fix(panel): react to changes on initial render (#871)
you can see this when there's a query which is applied from the URL (sorry, not relevant in the storybook) which makes the `canRefine` (`ais-Panel--noRefinement`) different than the default (`true`)
1 parent 6824079 commit 56917a0

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

src/mixins/__tests__/panel.test.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,18 @@ describe('createPanelConsumerMixin', () => {
108108
attributeName: false,
109109
};
110110

111-
expect(emitter.$emit).toHaveBeenCalledTimes(0);
111+
expect(emitter.$emit).toHaveBeenCalledTimes(1);
112+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, false);
112113

113114
wrapper.vm.state = {
114115
attributeName: true,
115116
};
116117

117-
expect(emitter.$emit).toHaveBeenCalledTimes(1);
118+
expect(emitter.$emit).toHaveBeenCalledTimes(2);
119+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, true);
118120
});
119121

120-
it('emits at least once when both values are set', () => {
122+
it('emits once when both values are set', () => {
121123
const localVue = createLocalVue();
122124
const emitter = createFakeEmitter();
123125
const Test = createFakeComponent(localVue);
@@ -137,7 +139,8 @@ describe('createPanelConsumerMixin', () => {
137139
attributeName: false,
138140
};
139141

140-
expect(emitter.$emit).toHaveBeenCalledTimes(0);
142+
expect(emitter.$emit).toHaveBeenCalledTimes(1);
143+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, false);
141144

142145
wrapper.vm.state = {
143146
attributeName: false,
@@ -146,7 +149,7 @@ describe('createPanelConsumerMixin', () => {
146149
expect(emitter.$emit).toHaveBeenCalledTimes(1);
147150
});
148151

149-
it('do not emit when the previous value is not set', () => {
152+
it('emits once on init of the component', () => {
150153
const localVue = createLocalVue();
151154
const emitter = createFakeEmitter();
152155
const Test = createFakeComponent(localVue);
@@ -166,7 +169,8 @@ describe('createPanelConsumerMixin', () => {
166169
attributeName: true,
167170
};
168171

169-
expect(emitter.$emit).not.toHaveBeenCalled();
172+
expect(emitter.$emit).toHaveBeenCalledTimes(1);
173+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, true);
170174
});
171175

172176
it('do not emit when the next value is not set', () => {
@@ -189,11 +193,12 @@ describe('createPanelConsumerMixin', () => {
189193
attributeName: true,
190194
};
191195

192-
expect(emitter.$emit).not.toHaveBeenCalled();
196+
expect(emitter.$emit).toHaveBeenCalledTimes(1);
197+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, true);
193198

194199
wrapper.vm.state = null;
195200

196-
expect(emitter.$emit).not.toHaveBeenCalled();
201+
expect(emitter.$emit).toHaveBeenCalledTimes(1);
197202
});
198203

199204
it('do not emit when the previous and next value are equal', () => {
@@ -216,18 +221,20 @@ describe('createPanelConsumerMixin', () => {
216221
attributeName: true,
217222
};
218223

219-
expect(emitter.$emit).not.toHaveBeenCalled();
224+
expect(emitter.$emit).toHaveBeenCalledTimes(1);
225+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, true);
220226

221227
wrapper.vm.state = {
222228
attributeName: false,
223229
};
224230

225-
expect(emitter.$emit).toHaveBeenCalledTimes(1);
231+
expect(emitter.$emit).toHaveBeenCalledTimes(2);
232+
expect(emitter.$emit).toHaveBeenLastCalledWith(PANEL_CHANGE_EVENT, false);
226233

227234
wrapper.vm.state = {
228235
attributeName: false,
229236
};
230237

231-
expect(emitter.$emit).toHaveBeenCalledTimes(1);
238+
expect(emitter.$emit).toHaveBeenCalledTimes(2);
232239
});
233240
});

src/mixins/panel.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,22 @@ export const createPanelConsumerMixin = ({ mapStateToCanRefine }) => ({
5858
};
5959
},
6060
watch: {
61-
state(nextState, previousState) {
62-
if (!previousState || !nextState) {
63-
return;
64-
}
61+
state: {
62+
immediate: true,
63+
handler(nextState, previousState) {
64+
if (!nextState) {
65+
return;
66+
}
6567

66-
const previousCanRefine = mapStateToCanRefine(previousState);
67-
const nextCanRefine = mapStateToCanRefine(nextState);
68+
const previousCanRefine = mapStateToCanRefine(previousState || {});
69+
const nextCanRefine = mapStateToCanRefine(nextState);
6870

69-
if (!this.hasAlreadyEmitted || previousCanRefine !== nextCanRefine) {
70-
this.emitter.$emit(PANEL_CHANGE_EVENT, nextCanRefine);
71+
if (!this.hasAlreadyEmitted || previousCanRefine !== nextCanRefine) {
72+
this.emitter.$emit(PANEL_CHANGE_EVENT, nextCanRefine);
7173

72-
this.hasAlreadyEmitted = true;
73-
}
74+
this.hasAlreadyEmitted = true;
75+
}
76+
},
7477
},
7578
},
7679
});

stories/Panel.stories.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { previewWrapper } from './utils';
44
storiesOf('ais-panel', module)
55
.addDecorator(previewWrapper())
66
.add('default', () => ({
7+
template: `
8+
<ais-panel>
9+
<template slot="header">Brand</template>
10+
<ais-refinement-list attribute="brand" />
11+
</ais-panel>
12+
`,
13+
}))
14+
.add('text content', () => ({
715
template: `
816
<ais-panel>
917
This is the body of the Panel.

0 commit comments

Comments
 (0)