2022-09-06 12:03:37 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2022-12-08 11:04:25 +00:00
|
|
|
import TerminalService from 'primevue/terminalservice';
|
2022-09-06 12:03:37 +00:00
|
|
|
import Terminal from './Terminal.vue';
|
|
|
|
|
|
|
|
describe('Terminal.vue', () => {
|
|
|
|
it('should return valid command', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
Terminal
|
|
|
|
},
|
|
|
|
template: `<Terminal welcomeMessage="Welcome to PrimeVue" prompt="primevue $" class="dark-demo-terminal" />`,
|
|
|
|
mounted() {
|
|
|
|
TerminalService.on('command', this.commandHandler);
|
|
|
|
},
|
|
|
|
beforeUnmount() {
|
|
|
|
TerminalService.off('command', this.commandHandler);
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
commandHandler(text) {
|
|
|
|
let response;
|
|
|
|
let argsIndex = text.indexOf(' ');
|
|
|
|
let command = argsIndex !== -1 ? text.substring(0, argsIndex) : text;
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
switch (command) {
|
|
|
|
case 'd':
|
2022-09-06 12:03:37 +00:00
|
|
|
response = 'Valid command';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-09-14 11:26:01 +00:00
|
|
|
response = 'Unknown command: ' + command;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TerminalService.emit('response', response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find('.p-terminal.p-component').exists()).toBe(true);
|
|
|
|
expect(wrapper.find('input.p-terminal-input').exists()).toBe(true);
|
|
|
|
|
|
|
|
wrapper.find('input.p-terminal-input').setValue('d');
|
|
|
|
|
|
|
|
wrapper.find('.p-terminal-input').trigger('keydown', {
|
2022-12-08 11:04:25 +00:00
|
|
|
key: 'Enter'
|
2022-09-06 12:03:37 +00:00
|
|
|
});
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
expect(wrapper.find('.p-terminal-response').text()).toBe('Valid command');
|
2022-12-09 06:47:41 +00:00
|
|
|
}, 50);
|
2022-09-06 12:03:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return invalid command', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
Terminal
|
|
|
|
},
|
|
|
|
template: `<Terminal welcomeMessage="Welcome to PrimeVue" prompt="primevue $" class="dark-demo-terminal" />`,
|
|
|
|
mounted() {
|
|
|
|
TerminalService.on('command', this.commandHandler);
|
|
|
|
},
|
|
|
|
beforeUnmount() {
|
|
|
|
TerminalService.off('command', this.commandHandler);
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
commandHandler(text) {
|
|
|
|
let response;
|
|
|
|
let argsIndex = text.indexOf(' ');
|
|
|
|
let command = argsIndex !== -1 ? text.substring(0, argsIndex) : text;
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
switch (command) {
|
|
|
|
case 'd':
|
2022-09-06 12:03:37 +00:00
|
|
|
response = 'Valid command';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-09-14 11:26:01 +00:00
|
|
|
response = 'Unknown command: ' + command;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TerminalService.emit('response', response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find('.p-terminal.p-component').exists()).toBe(true);
|
|
|
|
expect(wrapper.find('input.p-terminal-input').exists()).toBe(true);
|
|
|
|
|
|
|
|
wrapper.find('input.p-terminal-input').setValue('dd');
|
|
|
|
|
|
|
|
wrapper.find('.p-terminal-input').trigger('keydown', {
|
2022-12-08 11:04:25 +00:00
|
|
|
key: 'Enter'
|
2022-09-06 12:03:37 +00:00
|
|
|
});
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
expect(wrapper.find('.p-terminal-response').text()).toBe('Unknown command: dd');
|
2022-12-09 06:47:41 +00:00
|
|
|
}, 50);
|
2022-09-06 12:03:37 +00:00
|
|
|
});
|
2022-09-14 11:26:01 +00:00
|
|
|
});
|