Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert frontend MyInfo services to TypeScript #1233

Merged
merged 5 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ require('./modules/forms/services/form-error.client.factory.js')
require('./modules/forms/services/spcp-session.client.factory.js')
require('./modules/forms/services/spcp-redirect.client.factory.js')
require('./modules/forms/services/spcp-validate-esrvcid.client.factory.js')
require('./modules/forms/services/myinfo-redirect.client.factory.js')
require('./modules/forms/services/myinfo-validate-esrvcid.client.factory.js')
require('./modules/forms/services/submissions.client.factory.js')
require('./modules/forms/services/toastr.client.factory.js')
require('./modules/forms/services/attachment.client.service.js')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const dedent = require('dedent-js')
const MyInfoService = require('../../../../services/MyInfoService')

angular
.module('forms')
.controller('ActivateFormController', [
'$uibModalInstance',
'$timeout',
'$q',
'SpcpValidateEsrvcId',
'MyInfoValidateEsrvcId',
'externalScope',
'MailTo',
ActivateFormController,
Expand All @@ -15,8 +16,8 @@ angular
function ActivateFormController(
$uibModalInstance,
$timeout,
$q,
SpcpValidateEsrvcId,
MyInfoValidateEsrvcId,
externalScope,
MailTo,
) {
Expand Down Expand Up @@ -119,7 +120,8 @@ function ActivateFormController(
updateDisplay(null, { authType, esrvcId }, 0)
return Promise.resolve(true)
} else if (authType === 'MyInfo') {
return MyInfoValidateEsrvcId(target)
return $q
.when(MyInfoService.validateESrvcId(target))
.then((response) => {
if (response.isValid) {
updateDisplay(null, { authType, esrvcId })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const { cloneDeep } = require('lodash')

const MyInfoService = require('../../../../services/MyInfoService')
const {
getVisibleFieldIds,
getLogicUnitPreventingSubmit,
Expand All @@ -24,7 +24,7 @@ angular
.module('forms')
.directive('submitFormDirective', [
'$window',
'FormFields',
'$q',
'GTag',
'SpcpRedirect',
'SpcpSession',
Expand All @@ -35,13 +35,12 @@ angular
'$uibModal',
'$timeout',
'Verification',
'MyInfoRedirect',
submitFormDirective,
])

function submitFormDirective(
$window,
FormFields,
$q,
GTag,
SpcpRedirect,
SpcpSession,
Expand All @@ -52,7 +51,6 @@ function submitFormDirective(
$uibModal,
$timeout,
Verification,
MyInfoRedirect,
) {
return {
restrict: 'E',
Expand Down Expand Up @@ -94,9 +92,8 @@ function submitFormDirective(

scope.formLogin = function (authType, rememberMe) {
if (authType === 'MyInfo') {
return MyInfoRedirect({
formId: scope.form._id,
})
return $q
.when(MyInfoService.createRedirectURL(scope.form._id))
.then((response) => {
setReferrerToNull()
$window.location.href = response.redirectURL
Expand Down

This file was deleted.

This file was deleted.

31 changes: 31 additions & 0 deletions src/public/services/MyInfoService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axios from 'axios'

type RedirectURLResult = { redirectURL: string }

// Exported for testing
export const REDIRECT_URL_ENDPOINT = '/myinfo/redirect'
export const VALIDATE_ESRVCID_ENDPOINT = '/myinfo/validate'

export const createRedirectURL = (
formId: string,
): Promise<RedirectURLResult> => {
return axios
.get<RedirectURLResult>(REDIRECT_URL_ENDPOINT, {
params: { formId },
})
.then(({ data }) => data)
}

type LoginPageValidationResult =
| { isValid: true }
| { isValid: false; errorCode: string | null }

export const validateESrvcId = (
formId: string,
): Promise<LoginPageValidationResult> => {
return axios
.get<LoginPageValidationResult>(VALIDATE_ESRVCID_ENDPOINT, {
params: { formId },
})
.then(({ data }) => data)
}
93 changes: 93 additions & 0 deletions src/public/services/__tests__/MyInfoService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import axios from 'axios'
import { ObjectId } from 'bson'
import { mocked } from 'ts-jest/utils'

import * as MyInfoService from '../MyInfoService'

jest.mock('axios')
const MockAxios = mocked(axios, true)

const MOCK_REDIRECT_URL = 'redirectURL'
const MOCK_FORM_ID = new ObjectId().toHexString()

describe('MyInfoService', () => {
afterEach(() => jest.resetAllMocks())
describe('createRedirectURL', () => {
it('should return the redirect URL when retrieval succeeds', async () => {
const mockData = { redirectURL: MOCK_REDIRECT_URL }
MockAxios.get.mockResolvedValueOnce({ data: mockData })

const result = await MyInfoService.createRedirectURL(MOCK_FORM_ID)

expect(MockAxios.get).toHaveBeenCalledWith(
MyInfoService.REDIRECT_URL_ENDPOINT,
{
params: { formId: MOCK_FORM_ID },
},
)
expect(result).toEqual(mockData)
})

it('should reject with error when API call fails', async () => {
const error = new Error('rejected')
MockAxios.get.mockRejectedValueOnce(error)

const rejectFunction = () => MyInfoService.createRedirectURL(MOCK_FORM_ID)

await expect(rejectFunction).rejects.toThrowError(error)
expect(MockAxios.get).toHaveBeenCalledWith(
MyInfoService.REDIRECT_URL_ENDPOINT,
{
params: { formId: MOCK_FORM_ID },
},
)
})
})

describe('validateESrvcId', () => {
it('should return isValid as true when the e-service ID is valid', async () => {
const mockData = { isValid: true }
MockAxios.get.mockResolvedValueOnce({ data: mockData })

const result = await MyInfoService.validateESrvcId(MOCK_FORM_ID)

expect(MockAxios.get).toHaveBeenCalledWith(
MyInfoService.VALIDATE_ESRVCID_ENDPOINT,
{
params: { formId: MOCK_FORM_ID },
},
)
expect(result).toEqual(mockData)
})

it('should return isValid as false and an errorcode when the e-service ID is invalid', async () => {
const mockData = { isValid: false, errorCode: '123' }
MockAxios.get.mockResolvedValueOnce({ data: mockData })

const result = await MyInfoService.validateESrvcId(MOCK_FORM_ID)

expect(MockAxios.get).toHaveBeenCalledWith(
MyInfoService.VALIDATE_ESRVCID_ENDPOINT,
{
params: { formId: MOCK_FORM_ID },
},
)
expect(result).toEqual(mockData)
})

it('should reject with error when API call fails', async () => {
const error = new Error('rejected')
MockAxios.get.mockRejectedValueOnce(error)

const rejectFunction = () => MyInfoService.validateESrvcId(MOCK_FORM_ID)

await expect(rejectFunction).rejects.toThrowError(error)
expect(MockAxios.get).toHaveBeenCalledWith(
MyInfoService.VALIDATE_ESRVCID_ENDPOINT,
{
params: { formId: MOCK_FORM_ID },
},
)
})
})
})