-
Notifications
You must be signed in to change notification settings - Fork 87
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: migrate frontend routes and google analytics factory to ts #1405
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
396d35d
refactor: migrate google analytics factory to typescript
tshuli f28f571
chore: add tests for google analytics factory
tshuli bb7fe45
refactor: migrate frontend server routes and controller to ts
tshuli 22af466
refactor: update references
tshuli 7a4a95c
docs: frontend.server.routes
tshuli fccc0d2
chore: rename controller functions
tshuli f386afc
refactor: shift controller to frontend module
tshuli cf107fb
chore: shift prefix
tshuli 3a43433
chore: shift ga factory to frontend module
tshuli 1fea5ae
refactor: unknown instead of ParamsDictionary
tshuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/app/modules/frontend/__tests__/google-analytics.factory.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { StatusCodes } from 'http-status-codes' | ||
|
||
import { FeatureNames, RegisteredFeature } from 'src/config/feature-manager' | ||
|
||
import expressHandler from 'tests/unit/backend/helpers/jest-express' | ||
|
||
import { createGoogleAnalyticsFactory } from '../google-analytics.factory' | ||
|
||
describe('google-analytics.factory', () => { | ||
afterEach(() => jest.clearAllMocks()) | ||
const mockReq = expressHandler.mockRequest({ | ||
others: { | ||
app: { | ||
locals: { | ||
GATrackingID: 'abc', | ||
appName: 'xyz', | ||
environment: 'efg', | ||
}, | ||
}, | ||
}, | ||
}) | ||
const mockRes = expressHandler.mockResponse() | ||
|
||
it('should call res correctly if google-analytics feature is disabled', () => { | ||
const MOCK_DISABLED_GA_FEATURE: RegisteredFeature<FeatureNames.GoogleAnalytics> = { | ||
isEnabled: false, | ||
} | ||
|
||
const GoogleAnalyticsFactory = createGoogleAnalyticsFactory( | ||
MOCK_DISABLED_GA_FEATURE, | ||
) | ||
|
||
GoogleAnalyticsFactory.addGoogleAnalyticsData(mockReq, mockRes) | ||
|
||
expect(mockRes.type).toHaveBeenCalledWith('text/javascript') | ||
expect(mockRes.sendStatus).toHaveBeenCalledWith(StatusCodes.OK) | ||
expect(mockRes.send).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should call res correctly if google-analytics feature is enabled', () => { | ||
const MOCK_ENABLED_GA_FEATURE: RegisteredFeature<FeatureNames.GoogleAnalytics> = { | ||
isEnabled: true, | ||
} | ||
|
||
const GoogleAnalyticsFactory = createGoogleAnalyticsFactory( | ||
MOCK_ENABLED_GA_FEATURE, | ||
) | ||
|
||
GoogleAnalyticsFactory.addGoogleAnalyticsData(mockReq, mockRes) | ||
|
||
expect(mockRes.send).toHaveBeenCalledWith(expect.stringContaining('gtag')) | ||
expect(mockRes.type).toHaveBeenCalledWith('text/javascript') | ||
expect(mockRes.status).toHaveBeenCalledWith(StatusCodes.OK) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { celebrate, Joi, Segments } from 'celebrate' | ||
import { Router } from 'express' | ||
|
||
import * as FrontendServerController from './frontend.server.controller' | ||
import { GoogleAnalyticsFactory } from './google-analytics.factory' | ||
|
||
export const FrontendRouter = Router() | ||
|
||
/** | ||
* Generate the templated Javascript code for the frontend to initialise Google Tag Manager | ||
* Code depends on whether googleAnalyticsFeature.isEnabled | ||
* @route GET /frontend/datalayer | ||
* @return 200 when code generation is successful | ||
* @return 400 when code generation fails | ||
*/ | ||
FrontendRouter.get('/datalayer', GoogleAnalyticsFactory.addGoogleAnalyticsData) | ||
|
||
/** | ||
* Generate the templated Javascript code with environment variables for the frontend | ||
* @route GET /frontend/environment | ||
* @return 200 when code generation is successful | ||
* @return 400 when code generation fails | ||
*/ | ||
FrontendRouter.get('/environment', FrontendServerController.addEnvVarData) | ||
|
||
/** | ||
* Generate a json of current activated features | ||
* @route GET /frontend/features | ||
* @return json with featureManager.states | ||
*/ | ||
FrontendRouter.get('/features', FrontendServerController.showFeaturesStates) | ||
|
||
/** | ||
* Generate the javascript code to redirect to the correct url | ||
* @route GET /frontend/redirect | ||
* @return 200 when redirect code is successful | ||
* @return 400 when redirect code fails | ||
*/ | ||
FrontendRouter.get( | ||
'/redirect', | ||
celebrate({ | ||
[Segments.QUERY]: { | ||
redirectPath: Joi.string() | ||
.regex(/^[a-fA-F0-9]{24}(\/(preview|template|use-template))?/) | ||
.required(), | ||
}, | ||
}), | ||
FrontendServerController.generateRedirectUrl, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { RequestHandler } from 'express' | ||
import { ParamsDictionary } from 'express-serve-static-core' | ||
import { StatusCodes } from 'http-status-codes' | ||
|
||
import FeatureManager, { | ||
FeatureNames, | ||
RegisteredFeature, | ||
} from '../../../config/feature-manager' | ||
|
||
import * as FrontendServerController from './frontend.server.controller' | ||
|
||
interface IGoogleAnalyticsFactory { | ||
addGoogleAnalyticsData: RequestHandler< | ||
ParamsDictionary, | ||
string | { message: string } | ||
> | ||
} | ||
|
||
const googleAnalyticsFeature = FeatureManager.get(FeatureNames.GoogleAnalytics) | ||
|
||
/** | ||
* Factory function which returns the correct handler | ||
* for /frontend/datalayer endpoint depending on googleAnalyticsFeature.isEnabled | ||
* @param googleAnalyticsFeature | ||
*/ | ||
export const createGoogleAnalyticsFactory = ( | ||
googleAnalyticsFeature: RegisteredFeature<FeatureNames.GoogleAnalytics>, | ||
): IGoogleAnalyticsFactory => { | ||
if (!googleAnalyticsFeature.isEnabled) { | ||
return { | ||
addGoogleAnalyticsData: (req, res) => { | ||
res.type('text/javascript').sendStatus(StatusCodes.OK) | ||
}, | ||
} | ||
} | ||
return { | ||
addGoogleAnalyticsData: FrontendServerController.addGoogleAnalyticsData, | ||
} | ||
} | ||
|
||
export const GoogleAnalyticsFactory = createGoogleAnalyticsFactory( | ||
googleAnalyticsFeature, | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
module.exports = [ | ||
require('./frontend.server.routes.js'), | ||
require('./public-forms.server.routes.js'), | ||
] | ||
module.exports = [require('./public-forms.server.routes.js')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unknown
here as wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as above, ParamsDictionary is needed