Skip to content
Snippets Groups Projects
Verified Commit 550532f9 authored by Andreas Ellewsen's avatar Andreas Ellewsen
Browse files

Add more test coverage of frontend

parent b2a71661
No related branches found
No related tags found
1 merge request!101Add more test coverage of frontend
Pipeline #96489 passed
const OLD_ENV = process.env
beforeEach(() => {
jest.resetModules() // Most important - it clears the cache
process.env = { ...OLD_ENV } // Make a copy
})
afterAll(() => {
process.env = OLD_ENV // Restore old environment
})
test('Get theme uio', async () => {
process.env.REACT_APP_THEME = 'uio'
// eslint-disable-next-line global-require
const getTheme = require('./index').default
expect(getTheme().palette.primary.main).toEqual('#4977af')
})
test('Get theme uib', async () => {
process.env.REACT_APP_THEME = 'uib'
// eslint-disable-next-line global-require
const getTheme = require('./index').default
expect(getTheme().palette.primary.main).toEqual('#ba000d')
})
test('Get theme default when set', async () => {
process.env.REACT_APP_THEME = 'default'
// eslint-disable-next-line global-require
const getTheme = require('./index').default
expect(getTheme().palette.primary.main).toEqual('#3293ED')
})
test('Get theme default when unknown', async () => {
process.env.REACT_APP_THEME = 'unknown'
// eslint-disable-next-line global-require
const getTheme = require('./index').default
expect(getTheme().palette.primary.main).toEqual('#3293ED')
})
export {}
import { isValidEmail, isValidMobilePhoneNumber } from './index' import {
getCookie,
isValidEmail,
isValidFnr,
isValidMobilePhoneNumber,
maybeCsrfToken,
postJsonOpts,
} from './index'
// Mock i18next module to return a translation that just returns the key // Mock i18next module to return a translation that just returns the key
jest.mock('i18next', () => ({ jest.mock('i18next', () => ({
...@@ -9,6 +16,10 @@ test('Invalid phone number', async () => { ...@@ -9,6 +16,10 @@ test('Invalid phone number', async () => {
expect(isValidMobilePhoneNumber('dafasfdsfasdf')).not.toEqual(true) expect(isValidMobilePhoneNumber('dafasfdsfasdf')).not.toEqual(true)
}) })
test('Empty mobile phone number', async () => {
expect(isValidMobilePhoneNumber(undefined)).not.toEqual(true)
})
test('Valid phone number', async () => { test('Valid phone number', async () => {
expect(isValidMobilePhoneNumber('+47 97510000')).toEqual(true) expect(isValidMobilePhoneNumber('+47 97510000')).toEqual(true)
}) })
...@@ -25,3 +36,43 @@ test('Invalid e-mail', async () => { ...@@ -25,3 +36,43 @@ test('Invalid e-mail', async () => {
expect(isValidEmail('Øyvind.Åsen@example.org')).not.toEqual(true) expect(isValidEmail('Øyvind.Åsen@example.org')).not.toEqual(true)
expect(isValidEmail('Test.Tester@åsen.org')).not.toEqual(true) expect(isValidEmail('Test.Tester@åsen.org')).not.toEqual(true)
}) })
test('Body has values', async () => {
const data = { foo: 'bar' }
expect(postJsonOpts(data)).toEqual({
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: '{"foo":"bar"}',
credentials: 'same-origin',
})
})
test('Get cookie when set', async () => {
document.cookie = 'csrftoken=tokenvalue'
expect(maybeCsrfToken()).toEqual({ 'X-CSRFToken': 'tokenvalue' })
document.cookie = 'csrftoken= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'
})
test('Get null if cookie not set', async () => {
expect(maybeCsrfToken()).toEqual(null)
})
test('Get unknown cookie returns null', async () => {
document.cookie = 'csrftoken=tokenvalue'
expect(getCookie('foo')).toEqual(null)
document.cookie = 'csrftoken= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'
})
test('Valid fnr', async () => {
expect(isValidFnr('04026514903')).toEqual(true)
})
test('Null fnr', async () => {
expect(isValidFnr(undefined)).toEqual('common:validation.invalidIdNumber')
})
test('Invalid fnr', async () => {
expect(isValidFnr('')).toEqual('common:validation.invalidIdNumber')
})
...@@ -44,7 +44,7 @@ export function postJsonOpts(data: object): RequestInit { ...@@ -44,7 +44,7 @@ export function postJsonOpts(data: object): RequestInit {
} }
export function isValidFnr(data: string | undefined): boolean | string { export function isValidFnr(data: string | undefined): boolean | string {
if (data === null) { if (data === undefined) {
return i18n.t<string>('common:validation.invalidIdNumber').toString() return i18n.t<string>('common:validation.invalidIdNumber').toString()
} }
const valid = validator.idnr(data as string).status === 'valid' const valid = validator.idnr(data as string).status === 'valid'
...@@ -74,4 +74,4 @@ export function isValidEmail(data: string | undefined): boolean | string { ...@@ -74,4 +74,4 @@ export function isValidEmail(data: string | undefined): boolean | string {
return true return true
} }
return i18n.t<string>('common:validation.invalidEmail') return i18n.t<string>('common:validation.invalidEmail')
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment