Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lor-cab committed Oct 12, 2023
1 parent 701f2f7 commit 3a97f4a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getContext,
mockServiceMethod,
} from '../../../testUtils/unitTestHelpers';
import { generateRedirectForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage';
import { generateRedirectUrlForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage';
import getServerSideProps from './getServerSideProps';

jest.mock('next/dist/server/api-utils/node');
Expand Down Expand Up @@ -59,8 +59,8 @@ describe('getServerSideProps', () => {
getDefaultGrantMandatoryQuestion
);
(
generateRedirectForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue({ redirect: { destination: '/nextpage' } });
generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue('/nextpage');

const response = await getServerSideProps(getContext(getDefaultContext));

Expand All @@ -81,8 +81,8 @@ describe('getServerSideProps', () => {
getDefaultGrantMandatoryQuestion
);
(
generateRedirectForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue({ redirect: { destination: '/nextpage' } });
generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue('/nextpage');

await getServerSideProps(getContext(getDefaultContext));

Expand All @@ -109,8 +109,8 @@ describe('getServerSideProps', () => {
name: 'test name',
});
(
generateRedirectForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue({ redirect: { destination: '/nextpage' } });
generateRedirectUrlForMandatoryQuestionNextPage as jest.Mock
).mockReturnValue('/nextpage');
});

it('Should update the mandatoryQuestion', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '../../../services/GrantMandatoryQuestionService';
import { Optional } from '../../../testUtils/unitTestHelpers';
import callServiceMethod from '../../../utils/callServiceMethod';
import { generateRedirectForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage';
import { generateRedirectUrlForMandatoryQuestionNextPage } from '../../../utils/generateRedirectForMandatoryQuestionNextPage';
import { getJwtFromCookies } from '../../../utils/jwt';
import { routes } from '../../../utils/routes';

Expand All @@ -22,7 +22,7 @@ export default async function getServerSideProps({
const jwt = getJwtFromCookies(req);
const { publicRuntimeConfig } = getConfig();

let mandatoryQuestion;
let mandatoryQuestion: GrantMandatoryQuestionDto;
const grantMandatoryQuestionService =
GrantMandatoryQuestionService.getInstance();

Expand Down Expand Up @@ -50,13 +50,12 @@ export default async function getServerSideProps({
};
}

//only when someone access this page from the summary page,
//TODO only when someone access this page from the summary page,
// we want to show the default value
//otherwise we gonna send it to the next non filled page
generateRedirectForMandatoryQuestionNextPage(
const nextNotAnsweredPage = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
fromSummary === 'true'
mandatoryQuestionId
);

const response = await callServiceMethod(
Expand All @@ -68,11 +67,7 @@ export default async function getServerSideProps({
mandatoryQuestionId,
body
),
generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
fromSummary === 'true'
).redirect.destination,
nextNotAnsweredPage,
{
errorInformation:
'Something went wrong while trying to update your organisation details',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GrantMandatoryQuestionDto } from '../services/GrantMandatoryQuestionService';
import { generateRedirectForMandatoryQuestionNextPage } from './generateRedirectForMandatoryQuestionNextPage';
import { generateRedirectUrlForMandatoryQuestionNextPage } from './generateRedirectForMandatoryQuestionNextPage';

describe('generateRedirectForMandatoryQuestionNextPage', () => {
describe('generateRedirectUrlForMandatoryQuestionNextPage', () => {
it('should generate the correct URL for the address page when name is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
name: 'Example Organization',
Expand All @@ -16,13 +16,12 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
expect(result).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-address`
);
});
Expand All @@ -42,13 +41,12 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {

const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
expect(result).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-type`
);
});
Expand All @@ -67,13 +65,12 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
expect(result).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-companies-house-number`
);
});
Expand All @@ -92,13 +89,12 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
expect(result).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-charity-commission-number`
);
});
Expand All @@ -117,13 +113,12 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
expect(result).toBe(
`/mandatory-questions/mandatoryQuestionId/funding-amount`
);
});
Expand All @@ -142,13 +137,12 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
expect(result).toBe(
`/mandatory-questions/mandatoryQuestionId/funding-location`
);
});
Expand All @@ -167,14 +161,11 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
const result = generateRedirectUrlForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId,
false
mandatoryQuestionId
);

expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/summary`
);
expect(result).toBe(`/mandatory-questions/mandatoryQuestionId/summary`);
});
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { GrantMandatoryQuestionDto } from '../services/GrantMandatoryQuestionService';
import { routes } from './routes';

export const generateRedirectForMandatoryQuestionNextPage = (
export const generateRedirectUrlForMandatoryQuestionNextPage = (
mandatoryQuestion: GrantMandatoryQuestionDto,
mandatoryQuestionId: string,
isRequestFromSummaryPage: boolean
) => {
if (isRequestFromSummaryPage) {
return;
}
const redirect = {
destination: routes.mandatoryQuestions.summaryPage(mandatoryQuestionId),
permanent: false,
};
mandatoryQuestionId: string
): string => {
if (
mandatoryQuestion.name !== null &&
mandatoryQuestion.addressLine1 !== null &&
Expand All @@ -24,9 +16,7 @@ export const generateRedirectForMandatoryQuestionNextPage = (
mandatoryQuestion.fundingAmount !== null &&
mandatoryQuestion.fundingLocation !== null
) {
redirect.destination =
routes.mandatoryQuestions.summaryPage(mandatoryQuestionId);
return { redirect };
return routes.mandatoryQuestions.summaryPage(mandatoryQuestionId);
}
if (
mandatoryQuestion.name !== null &&
Expand All @@ -38,9 +28,7 @@ export const generateRedirectForMandatoryQuestionNextPage = (
mandatoryQuestion.charityCommissionNumber !== null &&
mandatoryQuestion.fundingAmount !== null
) {
redirect.destination =
routes.mandatoryQuestions.fundingLocationPage(mandatoryQuestionId);
return { redirect };
return routes.mandatoryQuestions.fundingLocationPage(mandatoryQuestionId);
}
if (
mandatoryQuestion.name !== null &&
Expand All @@ -51,9 +39,7 @@ export const generateRedirectForMandatoryQuestionNextPage = (
mandatoryQuestion.companiesHouseNumber !== null &&
mandatoryQuestion.charityCommissionNumber !== null
) {
redirect.destination =
routes.mandatoryQuestions.fundingAmountPage(mandatoryQuestionId);
return { redirect };
return routes.mandatoryQuestions.fundingAmountPage(mandatoryQuestionId);
}
if (
mandatoryQuestion.name !== null &&
Expand All @@ -63,11 +49,9 @@ export const generateRedirectForMandatoryQuestionNextPage = (
mandatoryQuestion.orgType !== null &&
mandatoryQuestion.companiesHouseNumber !== null
) {
redirect.destination =
routes.mandatoryQuestions.charityCommissionNumberPage(
mandatoryQuestionId
);
return { redirect };
return routes.mandatoryQuestions.charityCommissionNumberPage(
mandatoryQuestionId
);
}
if (
mandatoryQuestion.name !== null &&
Expand All @@ -76,23 +60,20 @@ export const generateRedirectForMandatoryQuestionNextPage = (
mandatoryQuestion.postcode !== null &&
mandatoryQuestion.orgType !== null
) {
redirect.destination =
routes.mandatoryQuestions.companiesHouseNumberPage(mandatoryQuestionId);
return { redirect };
return routes.mandatoryQuestions.companiesHouseNumberPage(
mandatoryQuestionId
);
}
if (
mandatoryQuestion.name !== null &&
mandatoryQuestion.addressLine1 !== null &&
mandatoryQuestion.city !== null &&
mandatoryQuestion.postcode !== null
) {
redirect.destination =
routes.mandatoryQuestions.typePage(mandatoryQuestionId);
return { redirect };
return routes.mandatoryQuestions.typePage(mandatoryQuestionId);
}
if (mandatoryQuestion.name !== null) {
redirect.destination =
routes.mandatoryQuestions.addressPage(mandatoryQuestionId);
return { redirect };
return routes.mandatoryQuestions.addressPage(mandatoryQuestionId);
}
return routes.mandatoryQuestions.summaryPage(mandatoryQuestionId);
};

0 comments on commit 3a97f4a

Please sign in to comment.