Skip to content

Commit 74b877a

Browse files
committed
Add test for promptLabel as a functioN
1 parent 79b3eb3 commit 74b877a

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/react-console.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ export interface LogEntry {
104104
}
105105

106106
export interface ConsoleProps{
107-
handler(command: string): any;
108-
cancel?(): any;
109-
complete?(words: string[], curr: number, promptText: string): string[];
110-
continue?(promptText: string): boolean;
107+
handler: (command: string)=>any;
108+
cancel?: ()=>any;
109+
complete?: (words: string[], curr: number, promptText: string)=>string[];
110+
continue?: (promptText: string)=>boolean;
111111
autofocus?: boolean;
112112
promptLabel?: string | (()=>string);
113113
welcomeMessage?: string;
@@ -168,7 +168,6 @@ export default class extends React.Component<ConsoleProps,ConsoleState> {
168168
promptLabel: '> ',
169169
continue: function() { return false; },
170170
cancel: function() {},
171-
handler: function() { this.return() },
172171
};
173172
child: {
174173
typer?: HTMLTextAreaElement;
@@ -190,12 +189,16 @@ export default class extends React.Component<ConsoleProps,ConsoleState> {
190189
log: log,
191190
}, this.scrollIfBottom() );
192191
}
193-
return = () => {
192+
done = () => {
194193
this.setState({
195194
acceptInput: true,
196195
currLabel: this.nextLabel(),
197196
}, this.scrollIfBottom() );
198197
}
198+
return = () => {
199+
console.log("WARNING: return() is deprecated. Use done() or the callback (2nd arg of handler) instead.");
200+
this.done();
201+
}
199202
// Component Lifecycle
200203
componentDidMount() {
201204
if(this.props.autofocus) {
@@ -528,7 +531,11 @@ export default class extends React.Component<ConsoleProps,ConsoleState> {
528531
lastCommand: ConsoleCommand.Default,
529532
}, () => {
530533
this.scrollToBottom();
531-
this.props.handler(command);
534+
if(this.props.handler) {
535+
this.props.handler(command)
536+
} else {
537+
this.done();
538+
}
532539
});
533540
}
534541
}

test/test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ describe('<Console />', function() {
138138
var wrapper = enzyme.mount(<Console promptLabel='ababa: '/>);
139139
expect(wrapper.find('.react-console-prompt-label').text()).equals('ababa: ');
140140
});
141+
it('Calls function when passed as promptLabel and uses returned values as labels', function() {
142+
var count = 0;
143+
function counter() {
144+
return count++;
145+
}
146+
var wrapper = enzyme.mount(<Console promptLabel={counter}/>);
147+
var typer = wrapper.find('.react-console-typer');
148+
typer.simulate('keyDown', { keyCode: 13 /* Return */ });
149+
typer.simulate('keyDown', { keyCode: 13 /* Return */ });
150+
typer.simulate('keyDown', { keyCode: 13 /* Return */ });
151+
var labels = wrapper.find('.react-console-prompt-label');
152+
expect(labels).length(4);
153+
expect(count).equals(4);
154+
expect(labels.first().text()).equals('0');
155+
expect(labels.last().text()).equals('3');
156+
});
141157
});
142158
describe('[Property] welcomeMessage: ', function () {
143159
it('Doesn\'t have class `react-console-welcome` when welcomeMessage undefined', function() {

0 commit comments

Comments
 (0)