|
| 1 | +import { mount, VueWrapper } from '@vue/test-utils'; |
| 2 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 3 | +import SchemaEditor from '@/components/SchemaEditor/SchemaEditor.vue'; |
| 4 | +import { Schema } from '@/domain/Schema.ts'; |
| 5 | +import { PropertyDefinitionList } from '@/domain/PropertyDefinitionList.ts'; |
| 6 | +import { createPropertyDefinitionFromJson } from '@/domain/PropertyDefinition.ts'; |
| 7 | +import { TextType } from '@/domain/propertyTypes/Text.ts'; |
| 8 | + |
| 9 | +function createWrapper( schema: Schema ): VueWrapper { |
| 10 | + return mount( SchemaEditor, { |
| 11 | + props: { |
| 12 | + initialSchema: schema, |
| 13 | + }, |
| 14 | + global: { |
| 15 | + stubs: { |
| 16 | + PropertyList: true, |
| 17 | + PropertyDefinitionEditor: true, |
| 18 | + }, |
| 19 | + }, |
| 20 | + } ); |
| 21 | +} |
| 22 | + |
| 23 | +describe( 'SchemaEditor', () => { |
| 24 | + |
| 25 | + beforeEach( () => { |
| 26 | + vi.stubGlobal( 'mw', { |
| 27 | + message: vi.fn( ( str ) => ( { |
| 28 | + text: () => str, |
| 29 | + parse: () => str, |
| 30 | + } ) ), |
| 31 | + } ); |
| 32 | + } ); |
| 33 | + |
| 34 | + it( 'selects the first property by default when properties exist', () => { |
| 35 | + const schema = new Schema( |
| 36 | + 'TestSchema', |
| 37 | + 'Description', |
| 38 | + new PropertyDefinitionList( [ |
| 39 | + createPropertyDefinitionFromJson( 'firstProp', { type: TextType.typeName } ), |
| 40 | + createPropertyDefinitionFromJson( 'secondProp', { type: TextType.typeName } ), |
| 41 | + ] ), |
| 42 | + ); |
| 43 | + |
| 44 | + const wrapper = createWrapper( schema ); |
| 45 | + |
| 46 | + expect( wrapper.classes() ).toContain( 'ext-neowiki-schema-editor--has-selected-property' ); |
| 47 | + expect( wrapper.findComponent( { name: 'PropertyList' } ).props( 'selectedPropertyName' ) ).toBe( 'firstProp' ); |
| 48 | + expect( wrapper.findComponent( { name: 'PropertyDefinitionEditor' } ).props( 'property' ).name.toString() ).toBe( 'firstProp' ); |
| 49 | + } ); |
| 50 | + |
| 51 | + it( 'does not select any property if schema has no properties', () => { |
| 52 | + const schema = new Schema( |
| 53 | + 'EmptySchema', |
| 54 | + 'Description', |
| 55 | + new PropertyDefinitionList( [] ), |
| 56 | + ); |
| 57 | + |
| 58 | + const wrapper = createWrapper( schema ); |
| 59 | + |
| 60 | + expect( wrapper.classes() ).not.toContain( 'ext-neowiki-schema-editor--has-selected-property' ); |
| 61 | + expect( wrapper.findComponent( { name: 'PropertyList' } ).props( 'selectedPropertyName' ) ).toBe( undefined ); |
| 62 | + expect( wrapper.findComponent( { name: 'PropertyDefinitionEditor' } ).exists() ).toBe( false ); |
| 63 | + } ); |
| 64 | +} ); |
0 commit comments