2022-11-25 23:54:43 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
2020-01-01 13:55:28 +05:30
|
|
|
import TokenedInput from '~/ide/components/shared/tokened_input.vue';
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
const TEST_PLACEHOLDER = 'Searching in test';
|
|
|
|
const TEST_TOKENS = [
|
|
|
|
{ label: 'lorem', id: 1 },
|
|
|
|
{ label: 'ipsum', id: 2 },
|
|
|
|
{ label: 'dolar', id: 3 },
|
|
|
|
];
|
|
|
|
const TEST_VALUE = 'lorem';
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
function getTokenElements(wrapper) {
|
|
|
|
return wrapper.findAll('.filtered-search-token button');
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
describe('IDE shared/TokenedInput', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
const createComponent = (props = {}) => {
|
|
|
|
wrapper = mount(TokenedInput, {
|
|
|
|
propsData: {
|
|
|
|
tokens: TEST_TOKENS,
|
|
|
|
placeholder: TEST_PLACEHOLDER,
|
|
|
|
value: TEST_VALUE,
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
attachTo: document.body,
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
2022-11-25 23:54:43 +05:30
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
it('renders tokens', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
createComponent();
|
|
|
|
const renderedTokens = getTokenElements(wrapper).wrappers.map((w) => w.text());
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(renderedTokens).toEqual(TEST_TOKENS.map((x) => x.label));
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('renders input', () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
createComponent();
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.find('input').element).toBeInstanceOf(HTMLInputElement);
|
|
|
|
expect(wrapper.find('input').element).toHaveValue(TEST_VALUE);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('renders placeholder, when tokens are empty', () => {
|
|
|
|
createComponent({ tokens: [] });
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.find('input').attributes('placeholder')).toBe(TEST_PLACEHOLDER);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('triggers "removeToken" on token click', async () => {
|
|
|
|
createComponent();
|
|
|
|
await getTokenElements(wrapper).at(0).trigger('click');
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('removeToken')[0]).toStrictEqual([TEST_TOKENS[0]]);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('removes token on backspace when value is empty', async () => {
|
|
|
|
createComponent({ value: '' });
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('removeToken')).toBeUndefined();
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
await wrapper.find('input').trigger('keyup.delete');
|
|
|
|
await wrapper.find('input').trigger('keyup.delete');
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('removeToken')[0]).toStrictEqual([TEST_TOKENS[TEST_TOKENS.length - 1]]);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('does not trigger "removeToken" on backspaces when value is not empty', async () => {
|
|
|
|
createComponent({ value: 'SOMETHING' });
|
|
|
|
|
|
|
|
await wrapper.find('input').trigger('keyup.delete');
|
|
|
|
await wrapper.find('input').trigger('keyup.delete');
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('removeToken')).toBeUndefined();
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('does not trigger "removeToken" on backspaces when tokens are empty', async () => {
|
|
|
|
createComponent({ value: '', tokens: [] });
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
await wrapper.find('input').trigger('keyup.delete');
|
|
|
|
await wrapper.find('input').trigger('keyup.delete');
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('removeToken')).toBeUndefined();
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('triggers "focus" on input focus', async () => {
|
|
|
|
createComponent();
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
await wrapper.find('input').trigger('focus');
|
|
|
|
|
|
|
|
expect(wrapper.emitted('focus')).toHaveLength(1);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('triggers "blur" on input blur', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await wrapper.find('input').trigger('blur');
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('blur')).toHaveLength(1);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('triggers "input" with value on input change', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await wrapper.find('input').setValue('something-else');
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(wrapper.emitted('input')[0]).toStrictEqual(['something-else']);
|
2018-11-18 11:00:15 +05:30
|
|
|
});
|
|
|
|
});
|