Add test for login failure
This commit is contained in:
parent
5c847b94e6
commit
380732d28a
@ -1,3 +1,4 @@
|
|||||||
export interface IResponse {
|
export interface IResponse {
|
||||||
data: any
|
data: any
|
||||||
|
status: number
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import * as Feature from './'
|
import * as Feature from './'
|
||||||
import {HTTPClientMock, TestUtils} from '../test-utils'
|
import {HTTPClientMock, TestUtils, getError} from '../test-utils'
|
||||||
import {IAPIDef} from '@rondo/common'
|
import {IAPIDef} from '@rondo/common'
|
||||||
import T from 'react-dom/test-utils'
|
import T from 'react-dom/test-utils'
|
||||||
|
|
||||||
const test = new TestUtils()
|
const test = new TestUtils()
|
||||||
|
|
||||||
describe('Login', () => {
|
describe('LoginForm', () => {
|
||||||
|
|
||||||
const http = new HTTPClientMock<IAPIDef>()
|
const http = new HTTPClientMock<IAPIDef>()
|
||||||
const loginActions = new Feature.LoginActions(http)
|
const loginActions = new Feature.LoginActions(http)
|
||||||
@ -55,6 +55,18 @@ describe('Login', () => {
|
|||||||
expect(onSuccess.mock.calls.length).toBe(1)
|
expect(onSuccess.mock.calls.length).toBe(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('sets the error message on failure', async () => {
|
||||||
|
http.mockAdd({
|
||||||
|
method: 'post',
|
||||||
|
url: '/auth/login',
|
||||||
|
data,
|
||||||
|
}, {error: 'test'}, 500)
|
||||||
|
T.Simulate.submit(node)
|
||||||
|
await getError(http.wait())
|
||||||
|
expect(node.querySelector('.error')!.textContent)
|
||||||
|
.toMatch(/HTTP Status: 500/i)
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
@ -24,6 +24,18 @@ describe('HTTPClientMock', () => {
|
|||||||
const error = await getError(http.get('/test'))
|
const error = await getError(http.get('/test'))
|
||||||
expect(error.message).toMatch(/mock/i)
|
expect(error.message).toMatch(/mock/i)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('can add a mock for custom status code', async () => {
|
||||||
|
http.mockClear().mockAdd(
|
||||||
|
{method: 'get', url: '/test'},
|
||||||
|
{error: 'Internal'}, 500)
|
||||||
|
|
||||||
|
const waitPromise = http.wait()
|
||||||
|
const error = await getError(http.get('/test'))
|
||||||
|
const error2 = await getError(waitPromise)
|
||||||
|
expect(error.message).toEqual('HTTP Status: 500')
|
||||||
|
expect(error2).toBe(error)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('await wait', () => {
|
describe('await wait', () => {
|
||||||
|
|||||||
@ -8,10 +8,22 @@ interface IReqRes {
|
|||||||
res: IResponse
|
res: IResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class HTTPClientError extends Error {
|
||||||
|
constructor(readonly request: IRequest, readonly response: IResponse) {
|
||||||
|
super('HTTP Status: ' + response.status)
|
||||||
|
Error.captureStackTrace(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class HTTPClientMock<T extends IRoutes> extends HTTPClient<T> {
|
export class HTTPClientMock<T extends IRoutes> extends HTTPClient<T> {
|
||||||
mocks: {[key: string]: any} = {}
|
mocks: {[key: string]: IResponse} = {}
|
||||||
requests: IRequest[] = []
|
requests: IRequest[] = []
|
||||||
|
|
||||||
|
protected waitPromise?: {
|
||||||
|
resolve: (r: IReqRes) => void
|
||||||
|
reject: (err: Error) => void
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
@ -30,33 +42,35 @@ export class HTTPClientMock<T extends IRoutes> extends HTTPClient<T> {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const data = this.mocks[key]
|
const res = this.mocks[key]
|
||||||
const res: IResponse = {data}
|
|
||||||
setImmediate(() => {
|
setImmediate(() => {
|
||||||
|
if (res.status >= 200 && res.status < 400) {
|
||||||
resolve(res)
|
resolve(res)
|
||||||
this.notify({req, res})
|
this.notify({req, res})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const error = new HTTPClientError(req, res)
|
||||||
|
reject(error)
|
||||||
|
this.notify(error)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serialize(req: IRequest) {
|
protected serialize(req: IRequest) {
|
||||||
return JSON.stringify(req, null, ' ')
|
return JSON.stringify(req, null, ' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
mockAdd(req: IRequest, res: any) {
|
mockAdd(req: IRequest, data: any, status = 200): this {
|
||||||
this.mocks[this.serialize(req)] = res
|
this.mocks[this.serialize(req)] = {data, status}
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
mockClear() {
|
mockClear(): this {
|
||||||
this.requests = []
|
this.requests = []
|
||||||
this.mocks = {}
|
this.mocks = {}
|
||||||
}
|
return this
|
||||||
|
|
||||||
protected waitPromise?: {
|
|
||||||
resolve: (r: IReqRes) => void
|
|
||||||
reject: (err: Error) => void
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected notify(r: IReqRes | Error) {
|
protected notify(r: IReqRes | Error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user