Skip to content

Commit 5602d99

Browse files
Merge pull request LucasBassetti#117 from Cyclodex/allowMetadataInStepsProps
Allowing metadata to appear in the props.steps
2 parents f9a3b4c + 14b042c commit 5602d99

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

lib/ChatBot.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class ChatBot extends Component {
193193
const steps = {};
194194

195195
for (let i = 0, len = previousSteps.length; i < len; i += 1) {
196-
const { id, message, value } = previousSteps[i];
197-
steps[id] = { id, message, value };
196+
const { id, message, value, metadata } = previousSteps[i];
197+
steps[id] = { id, message, value, metadata };
198198
}
199199

200200
return steps;
@@ -307,15 +307,15 @@ class ChatBot extends Component {
307307
const { previousSteps } = this.state;
308308

309309
const renderedSteps = previousSteps.map((step) => {
310-
const { id, message, value } = step;
311-
return { id, message, value };
310+
const { id, message, value, metadata } = step;
311+
return { id, message, value, metadata };
312312
});
313313

314314
const steps = [];
315315

316316
for (let i = 0, len = previousSteps.length; i < len; i += 1) {
317-
const { id, message, value } = previousSteps[i];
318-
steps[id] = { id, message, value };
317+
const { id, message, value, metadata } = previousSteps[i];
318+
steps[id] = { id, message, value, metadata };
319319
}
320320

321321
const values = previousSteps.filter(step => step.value).map(step => step.value);

tests/lib/ChatBot.spec.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import FloatButton from '../../lib/FloatButton';
88
import Header from '../../lib/Header';
99
import HeaderIcon from '../../lib/HeaderIcon';
1010
import { CloseIcon } from '../../lib/icons';
11-
// import { TextStep, OptionsStep, CustomStep } from '../../lib/steps';
11+
import { TextStep, OptionsStep, CustomStep } from '../../lib/steps';
1212

1313
const CustomComponent = () => (
1414
<div />
@@ -289,4 +289,50 @@ describe('ChatBot', () => {
289289
expect(wrapper.find('input.rsc-input')).to.have.length(0);
290290
});
291291
});
292+
293+
describe('Metadata', () => {
294+
const wrapper = mount(
295+
<ChatBot
296+
botDelay={0}
297+
steps={[
298+
{
299+
id: '1',
300+
message: 'Set metadata!',
301+
metadata: {
302+
custom: 'Hello World',
303+
},
304+
trigger: '2',
305+
},
306+
{
307+
id: '2',
308+
message: params => (params.steps[1].metadata.custom),
309+
end: true,
310+
},
311+
]}
312+
/>,
313+
);
314+
315+
before(() => {
316+
// Somehow it needs something like this, to wait for the application to be rendered.
317+
// TODO: improve this...
318+
wrapper.simulate('keyPress', { key: 'Enter' });
319+
});
320+
321+
after(() => {
322+
wrapper.unmount();
323+
});
324+
325+
it('should be accessible in "steps" and "previousStep"', () => {
326+
const bubbles = wrapper.find(TextStep);
327+
const step2Bubble = bubbles.at(1);
328+
expect(step2Bubble.props().previousStep.metadata.custom).to.be.equal('Hello World');
329+
expect(step2Bubble.props().steps[1].metadata.custom).to.be.equal('Hello World');
330+
});
331+
332+
it('should render in second bubble', () => {
333+
const bubbles = wrapper.find(TextStep);
334+
const step2Bubble = bubbles.at(1);
335+
expect(step2Bubble.text()).to.be.equal('Hello World');
336+
});
337+
});
292338
});

0 commit comments

Comments
 (0)