-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit-tests: KnownHostsEditDialog.test.ts, SSHKnownHosts.test.ts
Signed-off-by: Francesco Torchia <[email protected]>
- Loading branch information
Showing
4 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
shell/components/form/SSHKnownHosts/__tests__/KnownHostsEditDialog.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { mount, VueWrapper } from '@vue/test-utils'; | ||
import { _EDIT } from '@shell/config/query-params'; | ||
import KnownHostsEditDialog from '@shell/components/form/SSHKnownHosts/KnownHostsEditDialog.vue'; | ||
import CodeMirror from '@shell/components/CodeMirror.vue'; | ||
import FileSelector from '@shell/components/form/FileSelector.vue'; | ||
|
||
let wrapper: VueWrapper<InstanceType<typeof KnownHostsEditDialog>>; | ||
|
||
const mockedStore = () => { | ||
return { getters: { 'prefs/get': () => jest.fn() } }; | ||
}; | ||
|
||
const requiredSetup = () => { | ||
return { global: { mocks: { $store: mockedStore() } } }; | ||
}; | ||
|
||
describe('component: KnownHostsEditDialog', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<div id="modals"></div>'; | ||
wrapper = mount(KnownHostsEditDialog, { | ||
attachTo: document.body, | ||
props: { | ||
mode: _EDIT, | ||
value: 'line1\nline2\n', | ||
}, | ||
...requiredSetup(), | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
wrapper.unmount(); | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
it('should update text from CodeMirror', async() => { | ||
await wrapper.setData({ showModal: true }); | ||
|
||
expect(wrapper.vm.text).toBe('line1\nline2\n'); | ||
|
||
const codeMirror = wrapper.getComponent(CodeMirror); | ||
|
||
expect(codeMirror.element).toBeDefined(); | ||
|
||
await codeMirror.setData({ loaded: true }); | ||
|
||
// Emit CodeMirror value | ||
codeMirror.vm.$emit('onInput', 'bar'); | ||
await codeMirror.vm.$nextTick(); | ||
|
||
expect(wrapper.vm.text).toBe('bar'); | ||
}); | ||
|
||
it('should update text from FileSelector', async() => { | ||
await wrapper.setData({ showModal: true }); | ||
|
||
expect(wrapper.vm.text).toBe('line1\nline2\n'); | ||
|
||
const fileSelector = wrapper.getComponent(FileSelector); | ||
|
||
expect(fileSelector.element).toBeDefined(); | ||
|
||
// Emit Fileselector value | ||
fileSelector.vm.$emit('selected', 'foo'); | ||
await fileSelector.vm.$nextTick(); | ||
|
||
expect(wrapper.vm.text).toBe('foo'); | ||
}); | ||
|
||
it('should save changes and close dialog', async() => { | ||
await wrapper.setData({ | ||
showModal: true, | ||
text: 'foo', | ||
}); | ||
|
||
expect(wrapper.vm.value).toBe('line1\nline2\n'); | ||
expect(wrapper.vm.text).toBe('foo'); | ||
|
||
await wrapper.vm.closeDialog(true); | ||
|
||
expect((wrapper.emitted('closed') as any)[0][0].value).toBe('foo'); | ||
|
||
const dialog = wrapper.vm.$refs['sshKnownHostsDialog']; | ||
|
||
expect(dialog).toBeNull(); | ||
}); | ||
|
||
it('should discard changes and close dialog', async() => { | ||
await wrapper.setData({ | ||
showModal: true, | ||
text: 'foo', | ||
}); | ||
|
||
expect(wrapper.vm.value).toBe('line1\nline2\n'); | ||
expect(wrapper.vm.text).toBe('foo'); | ||
|
||
await wrapper.vm.closeDialog(false); | ||
|
||
expect((wrapper.emitted('closed') as any)[0][0].value).toBe('line1\nline2\n'); | ||
|
||
const dialog = wrapper.vm.$refs['sshKnownHostsDialog']; | ||
|
||
expect(dialog).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { _EDIT, _VIEW } from '@shell/config/query-params'; | ||
import SSHKnownHosts from '@shell/components/form/SSHKnownHosts/index.vue'; | ||
|
||
describe('component: SSHKnownHosts', () => { | ||
it.each([ | ||
['0 entities', '', 0], | ||
['0 entities (multiple empty lines)', '\n \n \n', 0], | ||
['1 entity', 'line1\n', 1], | ||
['1 entity (multiple empty lines)', 'line1\n\n\n', 1], | ||
['2 entities', 'line1\nline2\n', 2], | ||
['2 entities (multiple empty lines)', 'line1\n \n line2\n \n', 2], | ||
])('mode view: summary should be: %p', (_, value, entities) => { | ||
const wrapper = mount(SSHKnownHosts, { | ||
props: { | ||
mode: _VIEW, | ||
value, | ||
} | ||
}); | ||
|
||
const knownSshHostsSummary = wrapper.find('[data-testid="input-known-ssh-hosts_summary"]'); | ||
const knownSshHostsOpenDialog = wrapper.findAll('[data-testid="input-known-ssh-hosts_open-dialog"]'); | ||
|
||
expect(wrapper.vm.entries).toBe(entities); | ||
expect(knownSshHostsSummary.element).toBeDefined(); | ||
expect(knownSshHostsOpenDialog).toHaveLength(0); | ||
}); | ||
|
||
it('mode edit: should display summary and edit button', () => { | ||
const wrapper = mount(SSHKnownHosts, { | ||
props: { | ||
mode: _EDIT, | ||
value: 'line1\nline2\n', | ||
} | ||
}); | ||
|
||
const knownSshHostsSummary = wrapper.find('[data-testid="input-known-ssh-hosts_summary"]'); | ||
const knownSshHostsOpenDialog = wrapper.find('[data-testid="input-known-ssh-hosts_open-dialog"]'); | ||
|
||
expect(knownSshHostsSummary.element).toBeDefined(); | ||
expect(knownSshHostsOpenDialog.element).toBeDefined(); | ||
}); | ||
|
||
it('mode edit: should open edit dialog', async() => { | ||
const wrapper = mount(SSHKnownHosts, { | ||
props: { | ||
mode: _EDIT, | ||
value: '', | ||
} | ||
}); | ||
|
||
const knownSshHostsOpenDialog = wrapper.find('[data-testid="input-known-ssh-hosts_open-dialog"]'); | ||
const editDialog = wrapper.vm.$refs['editDialog'] as any; | ||
|
||
await knownSshHostsOpenDialog.trigger('click'); | ||
|
||
expect(editDialog.showModal).toBe(true); | ||
}); | ||
}); |