fix: 🐛 Fix failing tests
This commit is contained in:
parent
1230825571
commit
8d9cfe2de4
|
@ -0,0 +1,81 @@
|
||||||
|
import { CookieJar, Cookie as TCookie } from 'tough-cookie'
|
||||||
|
|
||||||
|
export interface Cookie {
|
||||||
|
name: string
|
||||||
|
value: string
|
||||||
|
path?: string
|
||||||
|
domain?: string
|
||||||
|
version?: string
|
||||||
|
expires?: string
|
||||||
|
secure?: boolean
|
||||||
|
httpOnly?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Cookies {
|
||||||
|
[key: string]: Cookie
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CookieManagerStatic {
|
||||||
|
set(url: string, cookie: Cookie, useWebKit?: boolean): Promise<boolean>
|
||||||
|
setFromResponse(url: string, cookie: string): Promise<boolean>
|
||||||
|
|
||||||
|
get(url: string, useWebKit?: boolean): Promise<Cookies>
|
||||||
|
|
||||||
|
clearAll(useWebKit?: boolean): Promise<boolean>
|
||||||
|
}
|
||||||
|
|
||||||
|
const convertTtoC = (cookie: string | TCookie): Cookie => {
|
||||||
|
if (typeof cookie === 'string') {
|
||||||
|
return convertTtoC(TCookie.parse(cookie) as TCookie)
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name: cookie.key,
|
||||||
|
value: cookie.value,
|
||||||
|
domain: cookie.domain || undefined,
|
||||||
|
expires:
|
||||||
|
cookie.expires === 'Infinity' ? undefined : cookie.expires.toUTCString(),
|
||||||
|
httpOnly: cookie.httpOnly || undefined,
|
||||||
|
path: cookie.path || undefined,
|
||||||
|
secure: cookie.secure,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const convertCtoT = (cookie: Cookie): TCookie =>
|
||||||
|
new TCookie({
|
||||||
|
key: cookie.name,
|
||||||
|
value: cookie.value,
|
||||||
|
domain: cookie.domain,
|
||||||
|
expires: cookie.expires ? new Date(cookie.expires) : undefined,
|
||||||
|
httpOnly: cookie.httpOnly || false,
|
||||||
|
path: cookie.path,
|
||||||
|
secure: cookie.secure || false,
|
||||||
|
})
|
||||||
|
const convertCookies = (cookies: TCookie[]): Cookies =>
|
||||||
|
cookies.reduce(
|
||||||
|
(map, cookie) => ({
|
||||||
|
...map,
|
||||||
|
[cookie.key]: convertTtoC(cookie),
|
||||||
|
}),
|
||||||
|
{} as Cookies
|
||||||
|
)
|
||||||
|
|
||||||
|
const jar = new CookieJar()
|
||||||
|
const CookieManager: CookieManagerStatic = {
|
||||||
|
clearAll: async () => {
|
||||||
|
await jar.removeAllCookies()
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
get: async (url) => {
|
||||||
|
const cookies = await jar.getCookies(url)
|
||||||
|
return convertCookies(cookies)
|
||||||
|
},
|
||||||
|
set: async (url, cookie) => {
|
||||||
|
await jar.setCookie(convertCtoT(cookie), url)
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
setFromResponse: async (url, cookie) => {
|
||||||
|
await jar.setCookie(cookie, url)
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CookieManager
|
|
@ -3,6 +3,10 @@ import React from 'react'
|
||||||
import { render } from '../../utils/testHelpers'
|
import { render } from '../../utils/testHelpers'
|
||||||
import { Auth } from '../auth.component'
|
import { Auth } from '../auth.component'
|
||||||
|
|
||||||
|
jest.mock('.../../data/schoolPlatforms', () => ({
|
||||||
|
...jest.requireActual('../../data/schoolPlatforms'),
|
||||||
|
}))
|
||||||
|
|
||||||
const setup = () => {
|
const setup = () => {
|
||||||
useApi.mockReturnValue({
|
useApi.mockReturnValue({
|
||||||
api: { on: jest.fn(), off: jest.fn() },
|
api: { on: jest.fn(), off: jest.fn() },
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import {
|
import {
|
||||||
Api,
|
Api,
|
||||||
|
Fetch,
|
||||||
FetcherOptions,
|
FetcherOptions,
|
||||||
RNCookieManager,
|
RNCookieManager,
|
||||||
ToughCookieJar,
|
ToughCookieJar,
|
||||||
|
@ -10,7 +11,7 @@ import { ApiHjarntorget } from './apiHjarntorget'
|
||||||
export { features } from './features'
|
export { features } from './features'
|
||||||
|
|
||||||
const init = (
|
const init = (
|
||||||
fetchImpl: typeof fetch,
|
fetchImpl: Fetch,
|
||||||
cookieManagerImpl: RNCookieManager | ToughCookieJar,
|
cookieManagerImpl: RNCookieManager | ToughCookieJar,
|
||||||
options?: FetcherOptions
|
options?: FetcherOptions
|
||||||
): Api => {
|
): Api => {
|
||||||
|
|
|
@ -3,6 +3,8 @@ import { ApiSkolplattformen } from './api'
|
||||||
import { Fetch, Headers, Response } from '@skolplattformen/api'
|
import { Fetch, Headers, Response } from '@skolplattformen/api'
|
||||||
import CookieManager from '@react-native-cookies/cookies'
|
import CookieManager from '@react-native-cookies/cookies'
|
||||||
|
|
||||||
|
jest.mock('@react-native-cookies/cookies')
|
||||||
|
|
||||||
describe('api', () => {
|
describe('api', () => {
|
||||||
let fetch: jest.Mocked<Fetch>
|
let fetch: jest.Mocked<Fetch>
|
||||||
let response: jest.Mocked<Response>
|
let response: jest.Mocked<Response>
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { ApiSkolplattformen } from './api'
|
||||||
export { features } from './features'
|
export { features } from './features'
|
||||||
|
|
||||||
const init = (
|
const init = (
|
||||||
fetchImpl: typeof fetch,
|
fetchImpl: Fetch,
|
||||||
cookieManagerImpl: RNCookieManager | ToughCookieJar,
|
cookieManagerImpl: RNCookieManager | ToughCookieJar,
|
||||||
options?: FetcherOptions
|
options?: FetcherOptions
|
||||||
): Api => {
|
): Api => {
|
||||||
|
|
Loading…
Reference in New Issue