Skip to content

Commit edf1032

Browse files
committed
updated nock and globals
1 parent d33bbd4 commit edf1032

4 files changed

Lines changed: 103 additions & 41 deletions

File tree

package-lock.json

Lines changed: 75 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@
109109
"eslint-plugin-n": "^17.21.3",
110110
"eslint-plugin-promise": "^7.2.1",
111111
"get-random-values": "^4.0.0",
112-
"globals": "^16.3.0",
112+
"globals": "^17.1.0",
113113
"isomorphic-fetch": "^3.0.0",
114114
"jest": "^30.2.0",
115115
"jest-environment-jsdom": "^30.0.5",
116116
"jsdom": "^27.4.0",
117117
"neostandard": "^0.12.2",
118-
"nock": "^13.5.6",
118+
"nock": "^15.0.0",
119119
"rdflib": "^2.3.0",
120120
"react": "^17.0.2",
121121
"react-dom": "^17.0.2",

test/helpers/setup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { toContainGraph } from '../custom-matchers/toContainGraph'
22
import { toEqualGraph } from '../custom-matchers/toEqualGraph'
33
import 'isomorphic-fetch'
44
import { TextEncoder, TextDecoder } from 'util'
5+
import { TransformStream, ReadableStream, WritableStream } from 'stream/web'
56

67
// https://stackoverflow.com/questions/52612122/how-to-use-jest-to-test-functions-using-crypto-or-window-mscrypto
78

@@ -14,6 +15,9 @@ global.crypto = {
1415

1516
global.TextEncoder = TextEncoder
1617
global.TextDecoder = TextDecoder
18+
global.TransformStream = TransformStream
19+
global.ReadableStream = ReadableStream
20+
global.WritableStream = WritableStream
1721

1822
// Mock external dependencies that solid-logic expects
1923
jest.mock('$rdf', () => require('rdflib'), { virtual: true })

test/unit/widgets/forms/autocomplete/autocomplete.test.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/^\/sparql/)
105+
.reply(200, fakeResponse, { 'Content-Type': 'application/sparql-results+json' })
106+
}
107+
99108
describe('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(/^\/sparql/)
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(/^\/sparql/)
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(/^\/sparql/)
345-
.reply(200, fakeResponse) // replyWithFile?
346351

347352
inputElement.value = 'mass'
348353

0 commit comments

Comments
 (0)