Skip to content

Commit

Permalink
[ASL-4495] Fix for window is undefined (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-horton-ho-sas authored Aug 9, 2024
1 parent 29777d3 commit 400223a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extends:
- "./node_modules/@ukhomeoffice/asl-eslint-common/index.js"
- "@ukhomeoffice/asl"

env:
es6: true
Expand Down
13 changes: 3 additions & 10 deletions client/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,9 @@ export const isTrainingLicence = values => {
};

export const getCurrentURLForFateOfAnimals = () => {
if (window.location.href) {
return window.location.href.split('/edit/')[0] + '/edit/fate-of-animals';
}
return null;
return typeof window !== 'undefined' ? window.location.href.split('/edit/')[0] + '/edit/fate-of-animals' : null;
};

export const UrlMarkdown = (linkText) => {
const url = getCurrentURLForFateOfAnimals();
if (url == null) {
return linkText;
}
return `[${linkText}](${url})`;
export const markdownLink = (linkText, url) => {
return url ? `[${linkText}](${url})` : linkText;
};
4 changes: 2 additions & 2 deletions client/schema/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import permissiblePurpose from './permissible-purpose';

import confirmProtocolsAffected from '../../helpers/confirm-protocols-affected';

import {isTrainingLicence, UrlMarkdown} from '../../helpers';
import {isTrainingLicence, markdownLink, getCurrentURLForFateOfAnimals} from '../../helpers';

export default () => {
return ({
Expand Down Expand Up @@ -1969,7 +1969,7 @@ export default () => {
{
name: 'fate',
label: 'What will happen to animals at the end of this protocol?',
hint: `Select all that apply. These options are based on what you selected in the ${UrlMarkdown('non-technical summary')}.`,
hint: `Select all that apply. These options are based on what you selected in the ${markdownLink('non-technical summary', getCurrentURLForFateOfAnimals())}.`,
type: 'checkbox',
preserveHierarchy: true,
className: 'smaller',
Expand Down
35 changes: 18 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asl/projects",
"version": "15.5.7",
"version": "15.5.8",
"description": "ASL PPL prototype",
"main": "client/external.js",
"styles": "assets/scss/projects.scss",
Expand Down Expand Up @@ -67,7 +67,7 @@
},
"devDependencies": {
"@babel/register": "^7.8.3",
"@ukhomeoffice/asl-eslint-common": "^2.2.0",
"@ukhomeoffice/eslint-config-asl": "^3.0.0",
"babel-eslint": "^10.0.1",
"eslint": "^7.32.0",
"eslint-plugin-import": "^2.29.1",
Expand Down
28 changes: 17 additions & 11 deletions test/specs/helpers/url-markdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import {getCurrentURLForFateOfAnimals, UrlMarkdown} from '../../../client/helpers';
import {getCurrentURLForFateOfAnimals, markdownLink} from '../../../client/helpers';

// Mock window object
global.window = {
Expand All @@ -17,26 +17,32 @@ describe('getCurrentURLForFateOfAnimals', () => {

it('should return null if window.location.href is not set', () => {
// Mock the href to be null
window.location.href = '';
const prevWindow = window;
// eslint-disable-next-line no-global-assign
window = undefined;

const result = getCurrentURLForFateOfAnimals();
assert.strictEqual(result, null);

// eslint-disable-next-line no-global-assign
window = prevWindow;
});
});

describe('UrlMarkdown', () => {
describe('markdownLink', () => {
it('should return the anchor name with the correct URL markdown', () => {
window.location.href = 'https://example.com/edit/some-page';
const anchoreName = 'Fate of Animals';
const anchorName = 'Fate of Animals';
const url = 'https://example.com/edit/fate-of-animals';
const expectedMarkdown = '[Fate of Animals](https://example.com/edit/fate-of-animals)';
const result = UrlMarkdown(anchoreName);

const result = markdownLink(anchorName, url);

assert.strictEqual(result, expectedMarkdown);
});

it('should return the anchor name if URL is null', () => {
// Mock the href to be null
window.location.href = '';
const anchoreName = 'Fate of Animals';
const result = UrlMarkdown(anchoreName);
assert.strictEqual(result, anchoreName);
const anchorName = 'Fate of Animals';
const result = markdownLink(anchorName, null);
assert.strictEqual(result, anchorName);
});
});

0 comments on commit 400223a

Please sign in to comment.