@@ -96,10 +96,25 @@ async function wait (ms) {
9696 } )
9797}
9898
99+ // Helper to set up default nock mocks for wikidata queries
100+ async function setupWikidataMock ( ) {
101+ const fakeResponse = await getFileContent ( 'test/unit/widgets/forms/autocomplete/broken-sparql-response-small.txt' )
102+ return nock ( 'https://query.wikidata.org' )
103+ . persist ( )
104+ . get ( / ^ \/ s p a r q l / )
105+ . reply ( 200 , fakeResponse , { 'Content-Type' : 'application/sparql-results+json' } )
106+ }
107+
99108describe ( 'autocompleteField' , ( ) => {
100109 let result
101110 beforeEach ( ( ) => {
102- // fetch.resetMocks();
111+ nock . cleanAll ( )
112+ nock . disableNetConnect ( )
113+ } )
114+
115+ afterEach ( ( ) => {
116+ nock . cleanAll ( )
117+ nock . enableNetConnect ( )
103118 } )
104119
105120 it ( 'exists as a function' , ( ) => {
@@ -186,6 +201,7 @@ describe('autocompleteField', () => {
186201 } )
187202
188203 it ( 'makes Cancel button appear when user inputs something' , async ( ) => {
204+ await setupWikidataMock ( )
189205 const container = document . createElement ( 'div' )
190206 const already = { }
191207 const callbackFunction = ( ) => { } // was jest.fn() // TODO: https://github.com/solidos/solid-ui/issues/263
@@ -212,6 +228,7 @@ describe('autocompleteField', () => {
212228 } )
213229
214230 it ( 'makes Cancel button work when user inputs something' , async ( ) => {
231+ await setupWikidataMock ( )
215232 const container = document . createElement ( 'div' )
216233 const already = { }
217234 const callbackFunction = ( ) => { } // was jest.fn() // TODO: https://github.com/solidos/solid-ui/issues/263
@@ -244,6 +261,7 @@ describe('autocompleteField', () => {
244261 } )
245262
246263 it ( 'on inpt fetches data (fixing wikidata timeout issue) making green table' , async ( ) => {
264+ await setupWikidataMock ( )
247265 const container = document . createElement ( 'div' )
248266 const already = { }
249267 const callbackFunction = ( ) => { } // was jest.fn() // TODO: https://github.com/solidos/solid-ui/issues/263
@@ -258,25 +276,21 @@ describe('autocompleteField', () => {
258276 callbackFunction
259277 )
260278 const inputElement = await findByTestId ( result , 'autocomplete-input' ) as HTMLInputElement
261- const fakeResponse = await getFileContent ( 'test/unit/widgets/forms/autocomplete/broken-sparql-response-small.txt' )
262-
263- nock ( 'https://query.wikidata.org' )
264- . get ( / ^ \/ s p a r q l / )
265- . reply ( 200 , fakeResponse ) // replyWithFile?
266279
267280 inputElement . value = 'mass'
268281
269282 const event1 = new Event ( 'input' )
270283 inputElement . dispatchEvent ( event1 )
271284
272285 const table = await findByTestId ( result , 'autocomplete-table' )
273- await waitFor ( ( ) => expect ( table . children . length ) . toEqual ( 4 ) )
286+ await waitFor ( ( ) => expect ( table . children . length ) . toEqual ( 4 ) , { timeout : 5000 } )
274287 expect ( table . children . length ) . toBeGreaterThan ( 1 )
275288 expect ( table ) . toMatchSnapshot ( )
276289 expect ( table . children [ 1 ] . style . color ) . toEqual ( 'rgb(0, 136, 0)' ) // green as all loaded
277290 } )
278291
279292 it ( 'typing more search term till unique selects the whole name and sets the accecpt button active' , async ( ) => {
293+ await setupWikidataMock ( )
280294 const container = document . createElement ( 'div' )
281295 const already = { }
282296 const callbackFunction = ( ) => { } // was jest.fn() // TODO: https://github.com/solidos/solid-ui/issues/263
@@ -291,11 +305,6 @@ describe('autocompleteField', () => {
291305 callbackFunction
292306 )
293307 const inputElement = await findByTestId ( result , 'autocomplete-input' ) as HTMLInputElement
294- const fakeResponse = await getFileContent ( 'test/unit/widgets/forms/autocomplete/broken-sparql-response-small.txt' )
295-
296- nock ( 'https://query.wikidata.org' )
297- . get ( / ^ \/ s p a r q l / )
298- . reply ( 200 , fakeResponse ) // replyWithFile?
299308
300309 inputElement . value = 'mass'
301310
@@ -324,6 +333,7 @@ describe('autocompleteField', () => {
324333 } )
325334
326335 it ( 'clicking on row of greenn table then accecpt button saves data' , async ( ) => {
336+ await setupWikidataMock ( )
327337 const container = document . createElement ( 'div' )
328338 const already = { }
329339 const callbackFunction = ( ) => { } // was jest.fn() // TODO: https://github.com/solidos/solid-ui/issues/263
@@ -338,11 +348,6 @@ describe('autocompleteField', () => {
338348 callbackFunction
339349 )
340350 const inputElement = await findByTestId ( result , 'autocomplete-input' ) as HTMLInputElement
341- const fakeResponse = await getFileContent ( 'test/unit/widgets/forms/autocomplete/broken-sparql-response-small.txt' )
342-
343- nock ( 'https://query.wikidata.org' )
344- . get ( / ^ \/ s p a r q l / )
345- . reply ( 200 , fakeResponse ) // replyWithFile?
346351
347352 inputElement . value = 'mass'
348353
0 commit comments