Skip to content

Commit 0df59a0

Browse files
committed
add function option to message step
1 parent cad25f2 commit 0df59a0

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

lib/steps/text/TextStep.jsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ class TextStep extends Component {
3232
}
3333

3434
renderMessage() {
35-
const { previousValue, step } = this.props;
35+
const {
36+
previousValue,
37+
step,
38+
steps,
39+
previousStep,
40+
triggerNextStep,
41+
} = this.props;
3642
const { component } = step;
3743
let { message } = step;
3844

3945
if (component) {
40-
const { steps, previousStep, triggerNextStep } = this.props;
4146
return React.cloneElement(component, {
4247
step,
4348
steps,
@@ -47,8 +52,9 @@ class TextStep extends Component {
4752
}
4853

4954
// Account for message being a callback which returns a string
50-
message = (typeof message === 'function') ? message() : message;
51-
message = message.replace(/{previousValue}/g, previousValue);
55+
message = (typeof message === 'function') ?
56+
message({ previousValue, steps }) :
57+
message.replace(/{previousValue}/g, previousValue);
5258

5359
return message;
5460
}

lib/storage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const JSON = require('circular-json');
22

3+
/* istanbul ignore next */
34
const getData = ({ cache, firstStep, steps }, callback) => {
45
const currentStep = firstStep;
56
const renderedSteps = [steps[currentStep.id]];
@@ -45,6 +46,7 @@ const getData = ({ cache, firstStep, steps }, callback) => {
4546
};
4647
};
4748

49+
/* istanbul ignore next */
4850
const setData = (data) => {
4951
localStorage.setItem('rsc_cache', JSON.stringify(data));
5052
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-simple-chatbot",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "React Simple Chatbot",
55
"main": "dist/react-simple-chatbot.js",
66
"scripts": {

tests/lib/ChatBot.spec.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ describe('ChatBot', () => {
176176
{
177177
id: '1',
178178
message: 'Hello World',
179+
trigger: '2',
180+
},
181+
{
182+
id: '2',
183+
message: 'Bye',
179184
end: true,
180185
},
181186
]}
@@ -196,6 +201,11 @@ describe('ChatBot', () => {
196201
wrapper.find(FloatButton).simulate('click');
197202
expect(wrapper.find(ChatBotContainer).props().opened).to.be.equal(true);
198203
});
204+
205+
it('should cache the steps', () => {
206+
const data = JSON.parse(localStorage.getItem('rsc_cache'));
207+
expect(data.renderedSteps.length).to.be.equal(2);
208+
});
199209
});
200210

201211
describe('Floating - Custom Opened', () => {
@@ -259,10 +269,5 @@ describe('ChatBot', () => {
259269
wrapper.find(FloatButton).simulate('click');
260270
expect(wrapper.find(ChatBotContainer).props().opened).to.be.equal(true);
261271
});
262-
263-
it('should cache the step', () => {
264-
const data = JSON.parse(localStorage.getItem('rsc_cache'));
265-
expect(data.renderedSteps.length).to.be.equal(1);
266-
});
267272
});
268273
});

tests/lib/TextStep.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,38 @@ describe('TextStep', () => {
133133
});
134134
});
135135

136+
describe('Function text', () => {
137+
const settings = {
138+
step: {
139+
id: '1',
140+
message: () => 'Hello',
141+
delay: 1000,
142+
user: true,
143+
bubbleColor: '#eee',
144+
fontColor: '#000',
145+
avatar: '',
146+
},
147+
isFirst: false,
148+
isLast: true,
149+
hideBotAvatar: false,
150+
hideUserAvatar: false,
151+
avatarStyle: {},
152+
bubbleStyle: {},
153+
triggerNextStep: () => {},
154+
};
155+
156+
const wrapper = mount(<TextStep {...settings} />);
157+
wrapper.setState({ loading: false });
158+
159+
it('should render bubble without avatar (not first)', () => {
160+
expect(wrapper.find(Image).exists()).to.be.equal(false);
161+
});
162+
163+
it('should render the message "Hello"', () => {
164+
expect(wrapper.find(Bubble).text()).to.be.equal('Hello');
165+
});
166+
});
167+
136168
describe('Component text', () => {
137169
const settings = {
138170
step: {

0 commit comments

Comments
 (0)