Skip to content

Commit

Permalink
fix: 馃悰 Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorlarsson committed Nov 27, 2021
1 parent 1230825 commit 8d9cfe2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
@@ -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
4 changes: 4 additions & 0 deletions apps/skolplattformen-sthlm/components/__tests__/Auth.test.js
Expand Up @@ -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() },
Expand Down
3 changes: 2 additions & 1 deletion libs/api-hjarntorget/lib/index.ts
@@ -1,5 +1,6 @@
import {
Api,
Fetch,
FetcherOptions,
RNCookieManager,
ToughCookieJar,
Expand All @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions libs/api-skolplattformen/lib/api.test.ts
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion libs/api-skolplattformen/lib/index.ts
Expand Up @@ -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 => {
Expand Down

0 comments on commit 8d9cfe2

Please sign in to comment.