From 0d6142f8885119a1e387e2bf7873bdc96f216dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tu=C4=9F=C3=A7e=20K=C3=BC=C3=A7=C3=BCko=C4=9Flu?= Date: Fri, 22 Apr 2022 14:11:26 +0300 Subject: [PATCH] Create Terminal.spec.js --- src/components/terminal/Terminal.spec.js | 97 ++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/components/terminal/Terminal.spec.js diff --git a/src/components/terminal/Terminal.spec.js b/src/components/terminal/Terminal.spec.js new file mode 100644 index 000000000..7d2de4114 --- /dev/null +++ b/src/components/terminal/Terminal.spec.js @@ -0,0 +1,97 @@ +import { mount } from '@vue/test-utils'; +import TerminalService from '@/components/terminalservice/TerminalService'; +import Terminal from './Terminal.vue'; + +describe('Terminal.vue', () => { + it('should return valid command', async () => { + const wrapper = mount({ + components: { + Terminal + }, + template: ``, + 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; + + switch(command) { + case "d": + response = 'Valid command'; + break; + + default: + response = "Unknown command: " + command; + } + + 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', { + keyCode: 13 + }); + + await wrapper.vm.$nextTick(); + + expect(wrapper.find('.p-terminal-response').text()).toBe('Valid command'); + }); + + it('should return invalid command', async () => { + const wrapper = mount({ + components: { + Terminal + }, + template: ``, + 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; + + switch(command) { + case "d": + response = 'Valid command'; + break; + + default: + response = "Unknown command: " + command; + } + + 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', { + keyCode: 13 + }); + + await wrapper.vm.$nextTick(); + + expect(wrapper.find('.p-terminal-response').text()).toBe('Unknown command: dd'); + }); +}); \ No newline at end of file