mirror of
				https://github.com/vuejs/babel-plugin-jsx.git
				synced 2025-11-01 01:42:21 +08:00 
			
		
		
		
	feat: add directive test case
add spread test case
This commit is contained in:
		
							
								
								
									
										58
									
								
								test/directive.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								test/directive.test.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										42
									
								
								test/spread.test.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										7
									
								
								test/util.js
									
									
									
									
									
										Normal 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 }; | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user