Skip to content

Commit a57440f

Browse files
alistair3149malberts
authored andcommitted
Select the first property on SchemaEditor load
1 parent c337b45 commit a57440f

3 files changed

Lines changed: 77 additions & 3 deletions

File tree

resources/ext.neowiki/src/components/SchemaEditor/PropertyList.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</template>
1313

1414
<script setup lang="ts">
15-
import { ref, computed } from 'vue';
15+
import { ref, computed, watch } from 'vue';
1616
import { CdxMenu, MenuItemData } from '@wikimedia/codex';
1717
import { cdxIconAdd } from '@wikimedia/codex-icons';
1818
import { PropertyDefinitionList } from '@/domain/PropertyDefinitionList.ts';
@@ -21,6 +21,7 @@ import { NeoWikiServices } from '@/NeoWikiServices.ts';
2121
2222
const props = defineProps<{
2323
properties: PropertyDefinitionList;
24+
selectedPropertyName?: string;
2425
}>();
2526
2627
const emit = defineEmits<{
@@ -31,7 +32,13 @@ const emit = defineEmits<{
3132
const componentRegistry = NeoWikiServices.getComponentRegistry();
3233
3334
// CdxMenu doesn't support passing a PropertyName as a value, so we use a string instead.
34-
const selectedValue = ref( '' );
35+
const selectedValue = ref( props.selectedPropertyName ?? '' );
36+
37+
watch( () => props.selectedPropertyName, ( newProperty ) => {
38+
if ( newProperty !== undefined ) {
39+
selectedValue.value = newProperty;
40+
}
41+
} );
3542
3643
const menuItems = computed( (): MenuItemData[] => [ ...props.properties ].map( ( property: PropertyDefinition ): MenuItemData => ( {
3744
label: property.name.toString(),

resources/ext.neowiki/src/components/SchemaEditor/SchemaEditor.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
>
66
<PropertyList
77
:properties="currentSchema.getPropertyDefinitions()"
8+
:selected-property-name="selectedPropertyName"
89
@property-selected="onPropertySelected"
910
@property-created="onPropertyCreated"
1011
/>
@@ -30,7 +31,9 @@ const props = defineProps<{
3031
}>();
3132

3233
const currentSchema = ref<Schema>( props.initialSchema );
33-
const selectedPropertyName = ref<string | undefined>( undefined );
34+
35+
const firstProperty = [ ...props.initialSchema.getPropertyDefinitions() ][ 0 ];
36+
const selectedPropertyName = ref<string | undefined>( firstProperty?.name.toString() );
3437

3538
const selectedProperty = computed( () => {
3639
if ( selectedPropertyName.value === undefined ) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)