fix: 🐛 Fix failing tests

This commit is contained in:
Viktor Sarström 2021-11-27 21:44:32 +01:00
parent 1230825571
commit 8d9cfe2de4
5 changed files with 90 additions and 2 deletions

View File

@ -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

View File

@ -3,6 +3,10 @@ import React from 'react'
import { render } from '../../utils/testHelpers'
import { Auth } from '../auth.component'
jest.mock('.../../data/schoolPlatforms', () => ({
...jest.requireActual('../../data/schoolPlatforms'),
}))
const setup = () => {
useApi.mockReturnValue({
api: { on: jest.fn(), off: jest.fn() },

View File

@ -1,5 +1,6 @@
import {
Api,
Fetch,
FetcherOptions,
RNCookieManager,
ToughCookieJar,
@ -10,7 +11,7 @@ import { ApiHjarntorget } from './apiHjarntorget'
export { features } from './features'
const init = (
fetchImpl: typeof fetch,
fetchImpl: Fetch,
cookieManagerImpl: RNCookieManager | ToughCookieJar,
options?: FetcherOptions
): Api => {

View File

@ -3,6 +3,8 @@ import { ApiSkolplattformen } from './api'
import { Fetch, Headers, Response } from '@skolplattformen/api'
import CookieManager from '@react-native-cookies/cookies'
jest.mock('@react-native-cookies/cookies')
describe('api', () => {
let fetch: jest.Mocked<Fetch>
let response: jest.Mocked<Response>

View File

@ -10,7 +10,7 @@ import { ApiSkolplattformen } from './api'
export { features } from './features'
const init = (
fetchImpl: typeof fetch,
fetchImpl: Fetch,
cookieManagerImpl: RNCookieManager | ToughCookieJar,
options?: FetcherOptions
): Api => {