feat: add directive test case

add spread test case
This commit is contained in:
zdw 2020-05-13 23:09:23 +08:00 committed by Amour1688
parent 751eabaae9
commit 8f18b5630a
3 changed files with 107 additions and 0 deletions

58
test/directive.test.js Normal file
View File

@ -0,0 +1,58 @@
import { defineComponent } from 'vue';
import { createMountedApp } from './util';
test('directive', () => {
const calls = [];
const customDirective = {
mounted() {
calls.push(1);
},
};
const compoentA = defineComponent(
{
directives: { customDirective },
render: () => (
<div
v-custom-directive={
{
value: 123,
modifiers: { modifier: true },
arg: 'arg',
}
} />
),
},
);
const { node } = createMountedApp(compoentA);
expect(calls).toEqual(expect.arrayContaining([1]));
expect(node.dirs).toHaveLength(1);
expect(node.dirs[0]).toMatchObject({
modifiers: { modifier: true }, dir: customDirective, arg: 'arg', value: 123,
});
});
test('directive in spread object property', () => {
const calls = [];
const customDirective = {
mounted() {
calls.push(1);
},
};
const directives = [
{
name: 'custom-directive', value: 123, modifiers: { modifier: true }, arg: 'arg',
},
];
const compoentA = defineComponent(
{
directives: { customDirective },
render: () => (<div {...{ directives }}>123</div>),
},
);
const { node } = createMountedApp(compoentA);
expect(calls).toEqual(expect.arrayContaining([1]));
expect(node.dirs).toHaveLength(1);
expect(node.dirs[0]).toMatchObject({
modifiers: { abc: true }, dir: customDirective, arg: 'arg', value: 123,
});
});

42
test/spread.test.js Normal file
View File

@ -0,0 +1,42 @@
import { createMountedApp } from './util';
test('Spread (single object expression)', () => {
const props = {
innerHTML: 123,
other: '1',
};
const { node, dom } = createMountedApp({ render: () => <div {...props}></div> });
expect(dom.innerHTML).toBe('<div other="1">123</div>');
expect(node.props.other).toBe('1');
});
test('Spread (mixed)', () => {
const calls = [];
const data = {
id: 'hehe',
onClick() {
calls.push(3);
},
innerHTML: 2,
class: ['a', 'b'],
};
const { node, dom } = createMountedApp({
setup: () => () => (
<div
href="huhu"
{...data}
class={{ c: true }}
on-click={() => calls.push(4)}
hook-insert={() => calls.push(2)}
/>
),
});
expect(node.props.id).toBe('hehe');
expect(node.props.href).toBe('huhu');
expect(node.props.class).toBe(['a', 'b', { c: true }]);
expect(node.props.innerHTML).toBe('2');
expect(calls).toBe([1, 2]);
dom.click();
expect(calls).toBe([1, 2, 3, 4]);
});

7
test/util.js Normal file
View File

@ -0,0 +1,7 @@
import { createApp } from 'vue';
export const createMountedApp = function (compoent) {
const dom = document.createElement('div');
const App = createApp(compoent).mount(dom);
return { App, dom, node: App.$.subTree };
};