test: correct fake tests in api-protocol-spec (#18869)

* fix protocol 404 fake tests

* fix another fake test

* fix last fake test (hopefully)
This commit is contained in:
Micha Hanselmann 2019-06-19 13:40:49 -07:00 committed by Shelley Vohr
parent 0146cc0eb5
commit 504edf2cf6
2 changed files with 11 additions and 10 deletions

View file

@ -112,7 +112,7 @@ describe('protocol module', () => {
it('sends error when callback is called with nothing', async () => { it('sends error when callback is called with nothing', async () => {
await registerBufferProtocol(protocolName, emptyHandler) await registerBufferProtocol(protocolName, emptyHandler)
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(404) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
it('does not crash when callback is called in next tick', async () => { it('does not crash when callback is called in next tick', async () => {
@ -160,9 +160,10 @@ describe('protocol module', () => {
}) })
it('fails when sending object other than string', async () => { it('fails when sending object other than string', async () => {
const handler = (request, callback) => callback(new Date()) const notAString = () => {}
const handler = (request, callback) => callback(notAString)
await registerStringProtocol(protocolName, handler) await registerStringProtocol(protocolName, handler)
expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
}) })
@ -198,7 +199,7 @@ describe('protocol module', () => {
it('fails when sending string', async () => { it('fails when sending string', async () => {
const handler = (request, callback) => callback(text) const handler = (request, callback) => callback(text)
await registerBufferProtocol(protocolName, handler) await registerBufferProtocol(protocolName, handler)
expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
}) })
@ -252,13 +253,13 @@ describe('protocol module', () => {
const fakeFilePath = path.join(fixtures, 'asar', 'a.asar', 'not-exist') const fakeFilePath = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
const handler = (request, callback) => callback(fakeFilePath) const handler = (request, callback) => callback(fakeFilePath)
await registerFileProtocol(protocolName, handler) await registerFileProtocol(protocolName, handler)
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(404) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
it('fails when sending unsupported content', async () => { it('fails when sending unsupported content', async () => {
const handler = (request, callback) => callback(new Date()) const handler = (request, callback) => callback(new Date())
await registerFileProtocol(protocolName, handler) await registerFileProtocol(protocolName, handler)
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(404) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
}) })
@ -282,13 +283,13 @@ describe('protocol module', () => {
it('fails when sending invalid url', async () => { it('fails when sending invalid url', async () => {
const handler = (request, callback) => callback({ url: 'url' }) const handler = (request, callback) => callback({ url: 'url' })
await registerHttpProtocol(protocolName, handler) await registerHttpProtocol(protocolName, handler)
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(404) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
it('fails when sending unsupported content', async () => { it('fails when sending unsupported content', async () => {
const handler = (request, callback) => callback(new Date()) const handler = (request, callback) => callback(new Date())
await registerHttpProtocol(protocolName, handler) await registerHttpProtocol(protocolName, handler)
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(404) await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
it('works when target URL redirects', async () => { it('works when target URL redirects', async () => {
@ -478,7 +479,7 @@ describe('protocol module', () => {
it('sends error when callback is called with nothing', async () => { it('sends error when callback is called with nothing', async () => {
await interceptStringProtocol('http', emptyHandler) await interceptStringProtocol('http', emptyHandler)
await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(404) await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(Error, '404')
}) })
}) })

View file

@ -12,7 +12,7 @@
resolve({data, status: request.status, headers: request.getAllResponseHeaders()}) resolve({data, status: request.status, headers: request.getAllResponseHeaders()})
} }
options.error = (xhr, errorType, error) => { options.error = (xhr, errorType, error) => {
reject(error ? error : xhr.status) reject(error ? error : new Error(`${xhr.status}`))
} }
$.ajax(options) $.ajax(options)
}) })