diff --git a/frontend/src/themes/index.test.ts b/frontend/src/themes/index.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..1322ee44b322e878aedf159da54757998a7abdda --- /dev/null +++ b/frontend/src/themes/index.test.ts @@ -0,0 +1,39 @@ +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 {} diff --git a/frontend/src/utils/index.test.ts b/frontend/src/utils/index.test.ts index b1bfa9734572b46cfe2798efc2cdcc5793daad89..66e4269a0348b66b5cf92c20f9568b1fcc8e54b5 100644 --- a/frontend/src/utils/index.test.ts +++ b/frontend/src/utils/index.test.ts @@ -1,4 +1,11 @@ -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 jest.mock('i18next', () => ({ @@ -9,6 +16,10 @@ test('Invalid phone number', async () => { expect(isValidMobilePhoneNumber('dafasfdsfasdf')).not.toEqual(true) }) +test('Empty mobile phone number', async () => { + expect(isValidMobilePhoneNumber(undefined)).not.toEqual(true) +}) + test('Valid phone number', async () => { expect(isValidMobilePhoneNumber('+47 97510000')).toEqual(true) }) @@ -25,3 +36,43 @@ test('Invalid e-mail', async () => { expect(isValidEmail('Øyvind.Åsen@example.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') +}) diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index c3737bd3f81c2d91eb6d1d04eeb0c2774a209042..8548f91bec1813ecdf4216958d5f5aba57c4a190 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -44,7 +44,7 @@ export function postJsonOpts(data: object): RequestInit { } export function isValidFnr(data: string | undefined): boolean | string { - if (data === null) { + if (data === undefined) { return i18n.t<string>('common:validation.invalidIdNumber').toString() } const valid = validator.idnr(data as string).status === 'valid' @@ -74,4 +74,4 @@ export function isValidEmail(data: string | undefined): boolean | string { return true } return i18n.t<string>('common:validation.invalidEmail') -} \ No newline at end of file +}