|
1 | 1 | import { mount, VueWrapper, flushPromises } from '@vue/test-utils'; |
2 | 2 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { nextTick } from 'vue'; |
3 | 4 | import SchemaLookup from '@/components/common/SchemaLookup.vue'; |
4 | 5 | import { createPinia, setActivePinia } from 'pinia'; |
5 | 6 | import { useSchemaStore } from '@/stores/SchemaStore.ts'; |
@@ -37,133 +38,177 @@ describe( 'SchemaLookup', () => { |
37 | 38 | schemaStore.searchAndFetchMissingSchemas = vi.fn().mockResolvedValue( [] ); |
38 | 39 | } ); |
39 | 40 |
|
40 | | - it( 'searches for schemas when input changes', () => { |
41 | | - const wrapper = mountComponent(); |
42 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 41 | + describe( 'searching', () => { |
| 42 | + it( 'searches for schemas when input changes', () => { |
| 43 | + const wrapper = mountComponent(); |
| 44 | + const lookup = wrapper.findComponent( CdxLookup ); |
43 | 45 |
|
44 | | - lookup.vm.$emit( 'input', 'test query' ); |
| 46 | + lookup.vm.$emit( 'input', 'test query' ); |
45 | 47 |
|
46 | | - expect( schemaStore.searchAndFetchMissingSchemas ).toHaveBeenCalledWith( 'test query' ); |
47 | | - } ); |
48 | | - |
49 | | - it( 'updates menu items with search results', async () => { |
50 | | - const mockResults = [ 'Schema1', 'Schema2' ]; |
51 | | - schemaStore.searchAndFetchMissingSchemas.mockResolvedValue( mockResults ); |
52 | | - schemaStore.schemas.set( 'Schema1', new Schema( 'Schema1', 'First description', new PropertyDefinitionList( [] ) ) ); |
53 | | - schemaStore.schemas.set( 'Schema2', new Schema( 'Schema2', 'Second description', new PropertyDefinitionList( [] ) ) ); |
| 48 | + expect( schemaStore.searchAndFetchMissingSchemas ).toHaveBeenCalledWith( 'test query' ); |
| 49 | + } ); |
54 | 50 |
|
55 | | - const wrapper = mountComponent(); |
56 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 51 | + it( 'updates menu items with search results', async () => { |
| 52 | + schemaStore.searchAndFetchMissingSchemas.mockResolvedValue( [ 'Schema1', 'Schema2' ] ); |
| 53 | + schemaStore.schemas.set( 'Schema1', new Schema( 'Schema1', 'First description', new PropertyDefinitionList( [] ) ) ); |
| 54 | + schemaStore.schemas.set( 'Schema2', new Schema( 'Schema2', 'Second description', new PropertyDefinitionList( [] ) ) ); |
57 | 55 |
|
58 | | - lookup.vm.$emit( 'input', 'test' ); |
59 | | - await flushPromises(); |
| 56 | + const wrapper = mountComponent(); |
| 57 | + const lookup = wrapper.findComponent( CdxLookup ); |
60 | 58 |
|
61 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [ |
62 | | - { label: 'Schema1', value: 'Schema1', description: 'First description' }, |
63 | | - { label: 'Schema2', value: 'Schema2', description: 'Second description' }, |
64 | | - ] ); |
65 | | - } ); |
| 59 | + lookup.vm.$emit( 'input', 'test' ); |
| 60 | + await flushPromises(); |
66 | 61 |
|
67 | | - it( 'omits description from menu items when schema has empty description', async () => { |
68 | | - schemaStore.searchAndFetchMissingSchemas.mockResolvedValue( [ 'WithDesc', 'NoDesc' ] ); |
69 | | - schemaStore.schemas.set( 'WithDesc', new Schema( 'WithDesc', 'Has a description', new PropertyDefinitionList( [] ) ) ); |
70 | | - schemaStore.schemas.set( 'NoDesc', new Schema( 'NoDesc', '', new PropertyDefinitionList( [] ) ) ); |
| 62 | + expect( lookup.props( 'menuItems' ) ).toEqual( [ |
| 63 | + { label: 'Schema1', value: 'Schema1', description: 'First description' }, |
| 64 | + { label: 'Schema2', value: 'Schema2', description: 'Second description' }, |
| 65 | + ] ); |
| 66 | + } ); |
71 | 67 |
|
72 | | - const wrapper = mountComponent(); |
73 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 68 | + it( 'omits description from menu items when schema has empty description', async () => { |
| 69 | + schemaStore.searchAndFetchMissingSchemas.mockResolvedValue( [ 'WithDesc', 'NoDesc' ] ); |
| 70 | + schemaStore.schemas.set( 'WithDesc', new Schema( 'WithDesc', 'Has a description', new PropertyDefinitionList( [] ) ) ); |
| 71 | + schemaStore.schemas.set( 'NoDesc', new Schema( 'NoDesc', '', new PropertyDefinitionList( [] ) ) ); |
74 | 72 |
|
75 | | - lookup.vm.$emit( 'input', 'test' ); |
76 | | - await flushPromises(); |
| 73 | + const wrapper = mountComponent(); |
| 74 | + const lookup = wrapper.findComponent( CdxLookup ); |
77 | 75 |
|
78 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [ |
79 | | - { label: 'WithDesc', value: 'WithDesc', description: 'Has a description' }, |
80 | | - { label: 'NoDesc', value: 'NoDesc', description: undefined }, |
81 | | - ] ); |
82 | | - } ); |
| 76 | + lookup.vm.$emit( 'input', 'test' ); |
| 77 | + await flushPromises(); |
83 | 78 |
|
84 | | - it( 'discards stale search results when a newer request completes first', async () => { |
85 | | - let resolveFirst: ( value: string[] ) => void; |
86 | | - const firstCallPromise = new Promise<string[]>( ( resolve ) => { |
87 | | - resolveFirst = resolve; |
| 79 | + expect( lookup.props( 'menuItems' ) ).toEqual( [ |
| 80 | + { label: 'WithDesc', value: 'WithDesc', description: 'Has a description' }, |
| 81 | + { label: 'NoDesc', value: 'NoDesc', description: undefined }, |
| 82 | + ] ); |
88 | 83 | } ); |
89 | 84 |
|
90 | | - schemaStore.searchAndFetchMissingSchemas = vi.fn() |
91 | | - .mockReturnValueOnce( firstCallPromise ) |
92 | | - .mockResolvedValueOnce( [ 'SecondSchema' ] ); |
| 85 | + it( 'discards stale search results when a newer request completes first', async () => { |
| 86 | + let resolveFirst: ( value: string[] ) => void; |
| 87 | + const firstCallPromise = new Promise<string[]>( ( resolve ) => { |
| 88 | + resolveFirst = resolve; |
| 89 | + } ); |
| 90 | + |
| 91 | + schemaStore.searchAndFetchMissingSchemas = vi.fn() |
| 92 | + .mockReturnValueOnce( firstCallPromise ) |
| 93 | + .mockResolvedValueOnce( [ 'SecondSchema' ] ); |
93 | 94 |
|
94 | | - schemaStore.schemas.set( 'FirstSchema', new Schema( 'FirstSchema', 'Stale', new PropertyDefinitionList( [] ) ) ); |
95 | | - schemaStore.schemas.set( 'SecondSchema', new Schema( 'SecondSchema', 'Fresh', new PropertyDefinitionList( [] ) ) ); |
| 95 | + schemaStore.schemas.set( 'FirstSchema', new Schema( 'FirstSchema', 'Stale', new PropertyDefinitionList( [] ) ) ); |
| 96 | + schemaStore.schemas.set( 'SecondSchema', new Schema( 'SecondSchema', 'Fresh', new PropertyDefinitionList( [] ) ) ); |
96 | 97 |
|
97 | | - const wrapper = mountComponent(); |
98 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 98 | + const wrapper = mountComponent(); |
| 99 | + const lookup = wrapper.findComponent( CdxLookup ); |
99 | 100 |
|
100 | | - lookup.vm.$emit( 'input', 'first' ); |
101 | | - lookup.vm.$emit( 'input', 'second' ); |
102 | | - await flushPromises(); |
| 101 | + lookup.vm.$emit( 'input', 'first' ); |
| 102 | + lookup.vm.$emit( 'input', 'second' ); |
| 103 | + await flushPromises(); |
103 | 104 |
|
104 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [ |
105 | | - { label: 'SecondSchema', value: 'SecondSchema', description: 'Fresh' }, |
106 | | - ] ); |
| 105 | + expect( lookup.props( 'menuItems' ) ).toEqual( [ |
| 106 | + { label: 'SecondSchema', value: 'SecondSchema', description: 'Fresh' }, |
| 107 | + ] ); |
107 | 108 |
|
108 | | - resolveFirst!( [ 'FirstSchema' ] ); |
109 | | - await flushPromises(); |
| 109 | + resolveFirst!( [ 'FirstSchema' ] ); |
| 110 | + await flushPromises(); |
110 | 111 |
|
111 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [ |
112 | | - { label: 'SecondSchema', value: 'SecondSchema', description: 'Fresh' }, |
113 | | - ] ); |
| 112 | + expect( lookup.props( 'menuItems' ) ).toEqual( [ |
| 113 | + { label: 'SecondSchema', value: 'SecondSchema', description: 'Fresh' }, |
| 114 | + ] ); |
| 115 | + } ); |
114 | 116 | } ); |
115 | 117 |
|
116 | | - it( 'reflects the selected prop on the lookup', () => { |
117 | | - const wrapper = mountComponent( { selected: 'Product' } ); |
118 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 118 | + describe( 'committing a selection', () => { |
| 119 | + it( 'emits the chosen schema when one is selected', () => { |
| 120 | + const wrapper = mountComponent(); |
| 121 | + const lookup = wrapper.findComponent( CdxLookup ); |
119 | 122 |
|
120 | | - expect( lookup.props( 'selected' ) ).toBe( 'Product' ); |
121 | | - expect( lookup.props( 'inputValue' ) ).toBe( 'Product' ); |
122 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [ { label: 'Product', value: 'Product' } ] ); |
123 | | - } ); |
| 123 | + lookup.vm.$emit( 'update:selected', 'Product' ); |
| 124 | + |
| 125 | + expect( wrapper.emitted( 'select' )?.[ 0 ] ).toEqual( [ 'Product' ] ); |
| 126 | + } ); |
| 127 | + |
| 128 | + it( 'does not emit while the selection is being changed by typing', () => { |
| 129 | + const wrapper = mountComponent( { selected: 'Product' } ); |
| 130 | + const lookup = wrapper.findComponent( CdxLookup ); |
124 | 131 |
|
125 | | - it( 'updates the lookup when the selected prop changes after mount', async () => { |
126 | | - const wrapper = mountComponent(); |
127 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 132 | + lookup.vm.$emit( 'update:input-value', 'Off' ); |
| 133 | + lookup.vm.$emit( 'update:selected', null ); |
128 | 134 |
|
129 | | - await wrapper.setProps( { selected: 'NewSchema' } ); |
| 135 | + expect( wrapper.emitted( 'select' ) ).toBeFalsy(); |
| 136 | + } ); |
130 | 137 |
|
131 | | - expect( lookup.props( 'selected' ) ).toBe( 'NewSchema' ); |
132 | | - expect( lookup.props( 'inputValue' ) ).toBe( 'NewSchema' ); |
133 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [ { label: 'NewSchema', value: 'NewSchema' } ] ); |
| 138 | + it( 'keeps a chosen schema untouched on blur', () => { |
| 139 | + const wrapper = mountComponent( { selected: 'Product' } ); |
| 140 | + const lookup = wrapper.findComponent( CdxLookup ); |
134 | 141 |
|
135 | | - await wrapper.setProps( { selected: null } ); |
| 142 | + lookup.vm.$emit( 'update:selected', 'Office' ); |
| 143 | + lookup.vm.$emit( 'update:input-value', 'Office' ); |
| 144 | + lookup.vm.$emit( 'blur' ); |
136 | 145 |
|
137 | | - expect( lookup.props( 'inputValue' ) ).toBe( '' ); |
138 | | - expect( lookup.props( 'menuItems' ) ).toEqual( [] ); |
| 146 | + expect( wrapper.emitted( 'select' ) ).toEqual( [ [ 'Office' ] ] ); |
| 147 | + } ); |
139 | 148 | } ); |
140 | 149 |
|
141 | | - it( 'emits the selected schema when one is chosen', () => { |
142 | | - const wrapper = mountComponent(); |
143 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 150 | + describe( 'rejecting invalid input', () => { |
| 151 | + it( 'reverts unmatched typed text to the committed schema on blur', async () => { |
| 152 | + const wrapper = mountComponent( { selected: 'Product' } ); |
| 153 | + const lookup = wrapper.findComponent( CdxLookup ); |
144 | 154 |
|
145 | | - lookup.vm.$emit( 'update:selected', 'Product' ); |
| 155 | + lookup.vm.$emit( 'update:selected', null ); |
| 156 | + lookup.vm.$emit( 'update:input-value', 'Produc' ); |
| 157 | + lookup.vm.$emit( 'blur' ); |
| 158 | + await nextTick(); |
146 | 159 |
|
147 | | - expect( wrapper.emitted( 'select' )?.[ 0 ] ).toEqual( [ 'Product' ] ); |
| 160 | + expect( lookup.props( 'inputValue' ) ).toBe( 'Product' ); |
| 161 | + expect( wrapper.emitted( 'select' ) ).toBeFalsy(); |
| 162 | + } ); |
148 | 163 | } ); |
149 | 164 |
|
150 | | - it( 'emits an empty selection when the lookup is cleared', () => { |
151 | | - const wrapper = mountComponent(); |
152 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 165 | + describe( 'clearing', () => { |
| 166 | + it( 'clears the selection when the field is emptied and blurred', () => { |
| 167 | + const wrapper = mountComponent( { selected: 'Product' } ); |
| 168 | + const lookup = wrapper.findComponent( CdxLookup ); |
153 | 169 |
|
154 | | - lookup.vm.$emit( 'update:selected', null ); |
| 170 | + lookup.vm.$emit( 'update:selected', null ); |
| 171 | + lookup.vm.$emit( 'update:input-value', '' ); |
| 172 | + lookup.vm.$emit( 'blur' ); |
| 173 | + |
| 174 | + expect( wrapper.emitted( 'select' )?.[ 0 ] ).toEqual( [ '' ] ); |
| 175 | + } ); |
155 | 176 |
|
156 | | - expect( wrapper.emitted( 'select' )?.[ 0 ] ).toEqual( [ '' ] ); |
| 177 | + it( 'does not emit a clear when an unset field is blurred', () => { |
| 178 | + const wrapper = mountComponent(); |
| 179 | + const lookup = wrapper.findComponent( CdxLookup ); |
| 180 | + |
| 181 | + lookup.vm.$emit( 'update:selected', null ); |
| 182 | + lookup.vm.$emit( 'update:input-value', '' ); |
| 183 | + lookup.vm.$emit( 'blur' ); |
| 184 | + |
| 185 | + expect( wrapper.emitted( 'select' ) ).toBeFalsy(); |
| 186 | + } ); |
157 | 187 | } ); |
158 | 188 |
|
159 | | - it( 'does not emit a clear while the user is typing a replacement', () => { |
160 | | - const wrapper = mountComponent( { selected: 'Product' } ); |
161 | | - const lookup = wrapper.findComponent( CdxLookup ); |
| 189 | + describe( 'reflecting the selected prop', () => { |
| 190 | + it( 'shows the selected schema in the field', () => { |
| 191 | + const wrapper = mountComponent( { selected: 'Product' } ); |
| 192 | + const lookup = wrapper.findComponent( CdxLookup ); |
162 | 193 |
|
163 | | - lookup.vm.$emit( 'update:input-value', 'Off' ); |
164 | | - lookup.vm.$emit( 'update:selected', null ); |
| 194 | + expect( lookup.props( 'selected' ) ).toBe( 'Product' ); |
| 195 | + expect( lookup.props( 'inputValue' ) ).toBe( 'Product' ); |
| 196 | + } ); |
| 197 | + |
| 198 | + it( 'updates the field when the selected prop changes', async () => { |
| 199 | + const wrapper = mountComponent(); |
| 200 | + const lookup = wrapper.findComponent( CdxLookup ); |
| 201 | + |
| 202 | + await wrapper.setProps( { selected: 'NewSchema' } ); |
165 | 203 |
|
166 | | - expect( wrapper.emitted( 'select' ) ).toBeFalsy(); |
| 204 | + expect( lookup.props( 'selected' ) ).toBe( 'NewSchema' ); |
| 205 | + expect( lookup.props( 'inputValue' ) ).toBe( 'NewSchema' ); |
| 206 | + |
| 207 | + await wrapper.setProps( { selected: null } ); |
| 208 | + |
| 209 | + expect( lookup.props( 'selected' ) ).toBe( null ); |
| 210 | + expect( lookup.props( 'inputValue' ) ).toBe( '' ); |
| 211 | + } ); |
167 | 212 | } ); |
168 | 213 |
|
169 | 214 | it( 'exposes focus method', () => { |
|
0 commit comments