Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

More tests for modules/

Jason Hurleyf4ed9b12bf5e
diff --git a/web/js/modules/alerts/actions.test.js b/web/js/modules/alerts/actions.test.js
new file mode 100644
index 000000000..5c1c4a818
--- /dev/null
+++ b/web/js/modules/alerts/actions.test.js
@@ -0,0 +1,105 @@
+import {
+ enableDDVZoomAlert,
+ enableDDVLocationAlert,
+ disableDDVZoomAlert,
+ disableDDVLocationAlert,
+} from './actions';
+
+jest.mock('./constants', () => ({
+ ACTIVATE_DDV_ZOOM_ALERT: 'ACTIVATE_DDV_ZOOM_ALERT',
+ ACTIVATE_DDV_LOCATION_ALERT: 'ACTIVATE_DDV_LOCATION_ALERT',
+ DEACTIVATE_DDV_ZOOM_ALERT: 'DEACTIVATE_DDV_ZOOM_ALERT',
+ DEACTIVATE_DDV_LOCATION_ALERT: 'DEACTIVATE_DDV_LOCATION_ALERT',
+}));
+
+describe('enableDDVZoomAlert', () => {
+ it('returns an action with type ACTIVATE_DDV_ZOOM_ALERT', () => {
+ const result = enableDDVZoomAlert('Zoom Alert');
+ expect(result.type).toBe('ACTIVATE_DDV_ZOOM_ALERT');
+ });
+
+ it('returns an action with the provided title', () => {
+ const result = enableDDVZoomAlert('Zoom Alert');
+ expect(result.title).toBe('Zoom Alert');
+ });
+
+ it('returns an action with an undefined title when none is provided', () => {
+ const result = enableDDVZoomAlert();
+ expect(result.title).toBeUndefined();
+ });
+
+ it('returns a plain object', () => {
+ const result = enableDDVZoomAlert('Zoom Alert');
+ expect(typeof result).toBe('object');
+ expect(typeof result.then).toBe('undefined');
+ });
+});
+
+describe('enableDDVLocationAlert', () => {
+ it('returns an action with type ACTIVATE_DDV_LOCATION_ALERT', () => {
+ const result = enableDDVLocationAlert('Location Alert');
+ expect(result.type).toBe('ACTIVATE_DDV_LOCATION_ALERT');
+ });
+
+ it('returns an action with the provided title', () => {
+ const result = enableDDVLocationAlert('Location Alert');
+ expect(result.title).toBe('Location Alert');
+ });
+
+ it('returns an action with an undefined title when none is provided', () => {
+ const result = enableDDVLocationAlert();
+ expect(result.title).toBeUndefined();
+ });
+
+ it('returns a plain object', () => {
+ const result = enableDDVLocationAlert('Location Alert');
+ expect(typeof result).toBe('object');
+ expect(typeof result.then).toBe('undefined');
+ });
+});
+
+describe('disableDDVZoomAlert', () => {
+ it('returns an action with type DEACTIVATE_DDV_ZOOM_ALERT', () => {
+ const result = disableDDVZoomAlert('Zoom Alert');
+ expect(result.type).toBe('DEACTIVATE_DDV_ZOOM_ALERT');
+ });
+
+ it('returns an action with the provided title', () => {
+ const result = disableDDVZoomAlert('Zoom Alert');
+ expect(result.title).toBe('Zoom Alert');
+ });
+
+ it('returns an action with an undefined title when none is provided', () => {
+ const result = disableDDVZoomAlert();
+ expect(result.title).toBeUndefined();
+ });
+
+ it('returns a plain object', () => {
+ const result = disableDDVZoomAlert('Zoom Alert');
+ expect(typeof result).toBe('object');
+ expect(typeof result.then).toBe('undefined');
+ });
+});
+
+describe('disableDDVLocationAlert', () => {
+ it('returns an action with type DEACTIVATE_DDV_LOCATION_ALERT', () => {
+ const result = disableDDVLocationAlert('Location Alert');
+ expect(result.type).toBe('DEACTIVATE_DDV_LOCATION_ALERT');
+ });
+
+ it('returns an action with the provided title', () => {
+ const result = disableDDVLocationAlert('Location Alert');
+ expect(result.title).toBe('Location Alert');
+ });
+
+ it('returns an action with an undefined title when none is provided', () => {
+ const result = disableDDVLocationAlert();
+ expect(result.title).toBeUndefined();
+ });
+
+ it('returns a plain object', () => {
+ const result = disableDDVLocationAlert('Location Alert');
+ expect(typeof result).toBe('object');
+ expect(typeof result.then).toBe('undefined');
+ });
+});
diff --git a/web/js/modules/charting/actions.test.js b/web/js/modules/charting/actions.test.js
new file mode 100644
index 000000000..9cea0017b
--- /dev/null
+++ b/web/js/modules/charting/actions.test.js
@@ -0,0 +1,222 @@
+import {
+ toggleChartingModeOnOff,
+ toggleChartingAOIOnOff,
+ updateRequestInProgressAction,
+ updateModalOpenAction,
+ toggleAOISelected,
+ updateChartingDateSelection,
+ updateChartingAOICoordinates,
+ changeChartingStartDate,
+ changeChartingEndDate,
+ updateActiveChartingLayerAction,
+} from './actions';
+
+jest.mock('./constants', () => ({
+ TOGGLE_ON_OFF: 'TOGGLE_ON_OFF',
+ TOGGLE_AOI_ON_OFF: 'TOGGLE_AOI_ON_OFF',
+ UPDATE_AOI_COORDINATES: 'UPDATE_AOI_COORDINATES',
+ TOGGLE_AOI_SELECTED_ON_OFF: 'TOGGLE_AOI_SELECTED_ON_OFF',
+ TOGGLE_REQUEST_IN_PROGRESS: 'TOGGLE_REQUEST_IN_PROGRESS',
+ TOGGLE_MODAL_OPEN: 'TOGGLE_MODAL_OPEN',
+ UPDATE_CHARTING_DATE_SELECTION: 'UPDATE_CHARTING_DATE_SELECTION',
+ UPDATE_START_DATE: 'UPDATE_START_DATE',
+ UPDATE_END_DATE: 'UPDATE_END_DATE',
+ UPDATE_ACTIVE_CHART: 'UPDATE_ACTIVE_CHART',
+}));
+
+describe('toggleChartingModeOnOff', () => {
+ it('dispatches TOGGLE_ON_OFF', () => {
+ const dispatch = jest.fn();
+ toggleChartingModeOnOff()(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_ON_OFF' });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ toggleChartingModeOnOff()(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('toggleChartingAOIOnOff', () => {
+ it('dispatches TOGGLE_AOI_ON_OFF', () => {
+ const dispatch = jest.fn();
+ toggleChartingAOIOnOff()(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_AOI_ON_OFF' });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ toggleChartingAOIOnOff()(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('updateRequestInProgressAction', () => {
+ it('dispatches TOGGLE_REQUEST_IN_PROGRESS with a true status', () => {
+ const dispatch = jest.fn();
+ updateRequestInProgressAction(true)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_REQUEST_IN_PROGRESS', status: true });
+ });
+
+ it('dispatches TOGGLE_REQUEST_IN_PROGRESS with a false status', () => {
+ const dispatch = jest.fn();
+ updateRequestInProgressAction(false)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_REQUEST_IN_PROGRESS', status: false });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ updateRequestInProgressAction(true)(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('updateModalOpenAction', () => {
+ it('dispatches TOGGLE_MODAL_OPEN with a true status', () => {
+ const dispatch = jest.fn();
+ updateModalOpenAction(true)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_MODAL_OPEN', status: true });
+ });
+
+ it('dispatches TOGGLE_MODAL_OPEN with a false status', () => {
+ const dispatch = jest.fn();
+ updateModalOpenAction(false)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_MODAL_OPEN', status: false });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ updateModalOpenAction(false)(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('toggleAOISelected', () => {
+ it('dispatches TOGGLE_AOI_SELECTED_ON_OFF with a provided action', () => {
+ const dispatch = jest.fn();
+ toggleAOISelected('some-action')(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_AOI_SELECTED_ON_OFF', action: 'some-action' });
+ });
+
+ it('dispatches TOGGLE_AOI_SELECTED_ON_OFF with null when no action is provided', () => {
+ const dispatch = jest.fn();
+ toggleAOISelected()(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_AOI_SELECTED_ON_OFF', action: null });
+ });
+
+ it('dispatches TOGGLE_AOI_SELECTED_ON_OFF with an explicit null action', () => {
+ const dispatch = jest.fn();
+ toggleAOISelected(null)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_AOI_SELECTED_ON_OFF', action: null });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ toggleAOISelected('some-action')(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('updateChartingDateSelection', () => {
+ it('dispatches UPDATE_CHARTING_DATE_SELECTION with the buttonClicked value', () => {
+ const dispatch = jest.fn();
+ updateChartingDateSelection('start')(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_CHARTING_DATE_SELECTION', buttonClicked: 'start' });
+ });
+
+ it('dispatches UPDATE_CHARTING_DATE_SELECTION with an alternate buttonClicked value', () => {
+ const dispatch = jest.fn();
+ updateChartingDateSelection('end')(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_CHARTING_DATE_SELECTION', buttonClicked: 'end' });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ updateChartingDateSelection('start')(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('updateChartingAOICoordinates', () => {
+ it('dispatches UPDATE_AOI_COORDINATES with the given extent', () => {
+ const dispatch = jest.fn();
+ const extent = [-180, -90, 180, 90];
+ updateChartingAOICoordinates(extent)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_AOI_COORDINATES', extent });
+ });
+
+ it('dispatches with an extent of null', () => {
+ const dispatch = jest.fn();
+ updateChartingAOICoordinates(null)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_AOI_COORDINATES', extent: null });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ updateChartingAOICoordinates([0, 0, 1, 1])(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('changeChartingStartDate', () => {
+ it('dispatches UPDATE_START_DATE with the given date', () => {
+ const dispatch = jest.fn();
+ const date = new Date('2024-01-01');
+ changeChartingStartDate(date)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_START_DATE', date });
+ });
+
+ it('dispatches UPDATE_START_DATE with a string date', () => {
+ const dispatch = jest.fn();
+ changeChartingStartDate('2024-01-01')(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_START_DATE', date: '2024-01-01' });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ changeChartingStartDate(new Date())(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('changeChartingEndDate', () => {
+ it('dispatches UPDATE_END_DATE with the given date', () => {
+ const dispatch = jest.fn();
+ const date = new Date('2024-12-31');
+ changeChartingEndDate(date)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_END_DATE', date });
+ });
+
+ it('dispatches UPDATE_END_DATE with a string date', () => {
+ const dispatch = jest.fn();
+ changeChartingEndDate('2024-12-31')(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_END_DATE', date: '2024-12-31' });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ changeChartingEndDate(new Date())(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('updateActiveChartingLayerAction', () => {
+ it('dispatches UPDATE_ACTIVE_CHART with the given layerId', () => {
+ const dispatch = jest.fn();
+ updateActiveChartingLayerAction('layer-123')(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_ACTIVE_CHART', layerId: 'layer-123' });
+ });
+
+ it('dispatches UPDATE_ACTIVE_CHART with a null layerId', () => {
+ const dispatch = jest.fn();
+ updateActiveChartingLayerAction(null)(dispatch);
+ expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_ACTIVE_CHART', layerId: null });
+ });
+
+ it('dispatches exactly once', () => {
+ const dispatch = jest.fn();
+ updateActiveChartingLayerAction('layer-123')(dispatch);
+ expect(dispatch).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/web/js/modules/link/util.test.js b/web/js/modules/link/util.test.js
index 955acd1e3..6166b2841 100644
--- a/web/js/modules/link/util.test.js
+++ b/web/js/modules/link/util.test.js
@@ -8,164 +8,204 @@ import {
wrapWithIframe,
} from './util';
+jest.mock('lodash', () => ({
+ each: jest.fn((collection, iteratee) => collection.forEach(iteratee)),
+}));
+
jest.mock('../date/util', () => ({
- serializeDate: jest.fn(() => '2024-01-15T00:00:00Z'),
+ serializeDate: jest.fn((date) => date.toISOString()),
}));
jest.mock('./constants', () => ({
ENCODING_EXCEPTIONS: [],
}));
+const { serializeDate } = require('../date/util');
+const { ENCODING_EXCEPTIONS } = require('./constants');
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ ENCODING_EXCEPTIONS.length = 0;
+});
+
describe('facebookUrlParams', () => {
- test('returns correctly encoded Facebook share URL', () => {
+ it('returns a correctly formed Facebook share URL', () => {
const result = facebookUrlParams('123', 'https://example.com', 'https://redirect.com', 'popup');
expect(result).toBe(
- `https://www.facebook.com/dialog/share?app_id=${encodeURIComponent('123')}&href=${encodeURIComponent('https://example.com')}&redirect_uri=${encodeURIComponent('https://redirect.com')}&display=${encodeURIComponent('popup')}`,
+ 'https://www.facebook.com/dialog/share?app_id=123&href=https%3A%2F%2Fexample.com&redirect_uri=https%3A%2F%2Fredirect.com&display=popup',
);
});
- test('encodes special characters in all params', () => {
- const result = facebookUrlParams('app&id', 'url with spaces', 'redir?x=1', 'dis play');
- expect(result).toContain(encodeURIComponent('app&id'));
- expect(result).toContain(encodeURIComponent('url with spaces'));
- expect(result).toContain(encodeURIComponent('redir?x=1'));
- expect(result).toContain(encodeURIComponent('dis play'));
+ it('encodes special characters in all parameters', () => {
+ const result = facebookUrlParams('app 1', 'https://a.com?x=1&y=2', 'https://r.com', 'page');
+ expect(result).toContain('app_id=app%201');
+ expect(result).toContain('href=https%3A%2F%2Fa.com%3Fx%3D1%26y%3D2');
});
});
describe('twitterUrlParams', () => {
- test('returns correctly encoded Twitter share URL', () => {
- const result = twitterUrlParams('https://example.com', 'Check this out!');
+ it('returns a correctly formed Twitter intent URL', () => {
+ const result = twitterUrlParams('https://example.com', 'Check this out');
expect(result).toBe(
- `https://twitter.com/intent/tweet?url=${encodeURIComponent('https://example.com')}&text=${encodeURIComponent('Check this out!')}`,
+ 'https://twitter.com/intent/tweet?url=https%3A%2F%2Fexample.com&text=Check%20this%20out',
);
});
+
+ it('encodes special characters in url and text', () => {
+ const result = twitterUrlParams('https://a.com?q=1&r=2', 'Hello & World');
+ expect(result).toContain('url=https%3A%2F%2Fa.com%3Fq%3D1%26r%3D2');
+ expect(result).toContain('text=Hello%20%26%20World');
+ });
});
describe('redditUrlParams', () => {
- test('returns correctly encoded Reddit share URL', () => {
- const result = redditUrlParams('https://example.com', 'Interesting Title');
+ it('returns a correctly formed Reddit submit URL', () => {
+ const result = redditUrlParams('https://example.com', 'Cool Title');
expect(result).toBe(
- `https://www.reddit.com/r/nasa/submit?url=${encodeURIComponent('https://example.com')}&title=${encodeURIComponent('Interesting Title')}`,
+ 'https://www.reddit.com/r/nasa/submit?url=https%3A%2F%2Fexample.com&title=Cool%20Title',
);
});
- test('encodes special characters', () => {
- const result = redditUrlParams('https://ex.com?q=test', 'Title & More');
- expect(result).toContain(encodeURIComponent('https://ex.com?q=test'));
- expect(result).toContain(encodeURIComponent('Title & More'));
+ it('encodes special characters in url and title', () => {
+ const result = redditUrlParams('https://a.com?x=1', 'Title & More');
+ expect(result).toContain('url=https%3A%2F%2Fa.com%3Fx%3D1');
+ expect(result).toContain('title=Title%20%26%20More');
});
});
describe('emailUrlParams', () => {
- test('returns correctly encoded mailto URL', () => {
- const result = emailUrlParams('Hello World', 'Check this link');
+ it('returns a correctly formed mailto URL', () => {
+ const result = emailUrlParams('Check this out', 'https://example.com');
expect(result).toBe(
- `mailto:?subject=${encodeURIComponent('Hello World')}&body=${encodeURIComponent('Check this link')}`,
+ 'mailto:?subject=Check%20this%20out&body=https%3A%2F%2Fexample.com',
);
});
- test('encodes special characters', () => {
- const result = emailUrlParams('Subject & More', 'Body with\nnewline');
- expect(result).toContain(encodeURIComponent('Subject & More'));
- expect(result).toContain(encodeURIComponent('Body with\nnewline'));
+ it('encodes special characters in subject and body', () => {
+ const result = emailUrlParams('Subject & More', 'Body with spaces & symbols');
+ expect(result).toContain('subject=Subject%20%26%20More');
+ expect(result).toContain('body=Body%20with%20spaces%20%26%20symbols');
});
});
describe('encode', () => {
- jest.mock('lodash', () => ({
- each: jest.fn((collection, iteratee) => {
- for (const key in collection) {
- if (Object.prototype.hasOwnProperty.call(collection, key)) {
- iteratee(collection[key], key);
- }
- }
- }),
- }));
- test('returns encodeURIComponent result when no exceptions apply', () => {
- expect(encode('hello world')).toBe(encodeURIComponent('hello world'));
- });
-
- test('encodes special URI characters', () => {
- expect(encode('a=b&c=d')).toBe(encodeURIComponent('a=b&c=d'));
+ it('returns URI-encoded value when no exceptions', () => {
+ const result = encode('hello world');
+ expect(result).toBe('hello%20world');
+ });
+
+ it('applies each encoding exception replacement', () => {
+ ENCODING_EXCEPTIONS.push({ match: /%20/g, replace: '+' });
+ const result = encode('hello world');
+ expect(result).toBe('hello+world');
+ });
+
+ it('applies multiple encoding exceptions in order', () => {
+ ENCODING_EXCEPTIONS.push({ match: /%20/g, replace: '+' });
+ ENCODING_EXCEPTIONS.push({ match: /\+/g, replace: '-' });
+ const result = encode('hello world');
+ expect(result).toBe('hello-world');
+ });
+
+ it('encodes special characters', () => {
+ const result = encode('a=b&c=d');
+ expect(result).toBe('a%3Db%26c%3Dd');
});
});
describe('wrapWithIframe', () => {
- test('wraps value in an iframe tag', () => {
+ it('returns an iframe tag with the given src', () => {
const result = wrapWithIframe('https://example.com');
- expect(result).toBe(
- '<iframe src="https://example.com" role="application" sandbox="allow-modals allow-scripts allow-same-origin allow-forms allow-popups" width="100%" height="100%" allow="fullscreen; autoplay;" loading="lazy"></iframe>',
- );
+ expect(result).toContain('src="https://example.com"');
});
- test('uses the provided src value', () => {
- const result = wrapWithIframe('https://other.com/path');
- expect(result).toContain('src="https://other.com/path"');
+ it('includes expected sandbox attributes', () => {
+ const result = wrapWithIframe('https://example.com');
+ expect(result).toContain('sandbox="allow-modals allow-scripts allow-same-origin allow-forms allow-popups"');
+ });
+
+ it('includes width and height of 100%', () => {
+ const result = wrapWithIframe('https://example.com');
+ expect(result).toContain('width="100%"');
+ expect(result).toContain('height="100%"');
+ });
+
+ it('includes allow fullscreen and autoplay', () => {
+ const result = wrapWithIframe('https://example.com');
+ expect(result).toContain('allow="fullscreen; autoplay;"');
+ });
+
+ it('includes lazy loading', () => {
+ const result = wrapWithIframe('https://example.com');
+ expect(result).toContain('loading="lazy"');
+ });
+
+ it('includes role="application"', () => {
+ const result = wrapWithIframe('https://example.com');
+ expect(result).toContain('role="application"');
});
});
describe('getPermalink', () => {
- test('adds time param when no querystring and t= not present', () => {
- const result = getPermalink('', new Date('2024-01-15'), false);
- expect(result).toContain('?t=');
+ const selectedDate = new Date('2024-01-15T00:00:00.000Z');
+
+ beforeEach(() => {
+ serializeDate.mockReturnValue('2024-01-15T00:00:00.000Z');
});
- test('appends time param to existing querystring without t=', () => {
- // setHref(`${BASE_URL}?layers=MODIS`);
- const result = getPermalink('?layers=MODIS', new Date('2024-01-15'), false);
- expect(result).toContain('t=');
- expect(result).toContain('layers=MODIS');
+ it('builds a permalink with time param when no queryString and t= not present', () => {
+ const result = getPermalink('', selectedDate, false);
+ expect(result).toContain('?t=');
});
- test('adds em=true when isEmbed is true', () => {
- const result = getPermalink('', new Date(), true);
- expect(result).toContain('em=true');
+ it('appends time param when queryString exists but has no t=', () => {
+ const result = getPermalink('?v=1,2,3,4', selectedDate, false);
+ expect(result).toContain('&t=');
+ expect(result).toContain('?v=1,2,3,4');
});
- test('changes em=true to em=false when isEmbed is false', () => {
- const result = getPermalink('?em=true', new Date(), false);
- expect(result).toContain('em=false');
+ it('appends &em=true when isEmbed is true', () => {
+ const result = getPermalink('', selectedDate, true);
+ expect(result).toContain('&em=true');
});
- test('removes &kiosk=true from permalink', () => {
- const result = getPermalink('?layers=MODIS&kiosk=true&t=2024', new Date(), false);
- expect(result).not.toContain('kiosk=true');
+ it('appends &em=true when isEmbed is false', () => {
+ const result = getPermalink('&em=false', selectedDate, false);
+ expect(result).toContain('&em=false');
});
- test('removes ?kiosk=true (only param) from permalink', () => {
- const result = getPermalink('?kiosk=true&t=2024', new Date(), false);
+ it('removes &kiosk=true from permalink', () => {
+ const result = getPermalink('?t=2024&kiosk=true', selectedDate, false);
expect(result).not.toContain('kiosk=true');
});
- test('removes &kiosk=true (only param) from permalink', () => {
- const result = getPermalink('?kiosk=true&t=2024', new Date(), false);
+ it('removes ?kiosk=true (only param) from permalink', () => {
+ const result = getPermalink('?kiosk=true', selectedDate, false);
expect(result).not.toContain('kiosk=true');
});
- test('removes &e2e=true from permalink', () => {
- const result = getPermalink('?layers=MODIS&e2e=true&t=2024', new Date(), false);
+ it('removes &e2e=true from permalink', () => {
+ const result = getPermalink('?t=2024&e2e=true', selectedDate, false);
expect(result).not.toContain('e2e=true');
});
- test('removes ?e2e=true (only param) from permalink', () => {
- const result = getPermalink('?e2e=true&t=2024', new Date(), false);
- expect(result).not.toContain('?e2e=true&');
+ it('removes ?e2e=true (only param) from permalink', () => {
+ const result = getPermalink('?e2e=true', selectedDate, false);
+ expect(result).not.toContain('e2e=true');
});
- test('removes eic= pattern from permalink', () => {
- const result = getPermalink('?layers=MODIS&eic=AB&t=2024', new Date(), false);
- expect(result).not.toContain('eic=');
+ it('removes eic= and its two trailing characters', () => {
+ const result = getPermalink('?t=2024&eic=AB', selectedDate, false);
+ expect(result).not.toContain('eic=AB');
});
- test('cleans up trailing & after eic removal', () => {
- const result = getPermalink('?t=2024&eic=AB', new Date(), false);
+ it('cleans up trailing & after eic= removal', () => {
+ const result = getPermalink('?t=2024&eic=AB', selectedDate, false);
expect(result).not.toMatch(/&$/);
- expect(result).not.toContain('eic=');
});
- test('cleans up double && after eic removal', () => {
- const result = getPermalink('?layers=MODIS&eic=AB&t=2024', new Date(), false);
+ it('cleans up double && after eic= removal', () => {
+ const result = getPermalink('?t=2024&eic=AB&v=1', selectedDate, false);
expect(result).not.toContain('&&');
});
});
diff --git a/web/js/modules/product-picker/util.test.js b/web/js/modules/product-picker/util.test.js
index 4072f4f95..e5eeb3174 100644
--- a/web/js/modules/product-picker/util.test.js
+++ b/web/js/modules/product-picker/util.test.js
@@ -1,3 +1,258 @@
-test('util module test', () => {
+import {
+ getRecentLayers,
+ clearRecentLayers,
+ clearSingleRecentLayer,
+ updateRecentLayers,
+ recentLayerInfo,
+} from './util';
+import safeLocalStorage from '../../util/local-storage';
+import util from '../../util/util';
+
+jest.mock('../../util/local-storage', () => ({
+ keys: { RECENT_LAYERS: 'RECENT_LAYERS' },
+ getItem: jest.fn(),
+ setItem: jest.fn(),
+ removeItem: jest.fn(),
+ enabled: true,
+}));
+
+jest.mock('../../util/util', () => ({
+ now: jest.fn(),
+}));
+
+const PROJ = 'geographic';
+const ALL_PROJECTIONS = ['geographic', 'arctic', 'antarctic'];
+
+function makeLayerEntry(id, count, dateAdded) {
+ return { id, count, dateAdded };
+}
+
+function makeStoredLayers(overrides = {}) {
+ return {
+ geographic: [],
+ arctic: [],
+ antarctic: [],
+ ...overrides,
+ };
+}
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ safeLocalStorage.enabled = true;
+ util.now.mockReturnValue({ valueOf: () => 1000 });
+});
+
+describe('recentLayerInfo', () => {
+ it('is a non-empty string', () => {
+ expect(typeof recentLayerInfo).toBe('string');
+ expect(recentLayerInfo.length).toBeGreaterThan(0);
+ });
+
+ it('mentions frequency of use', () => {
+ expect(recentLayerInfo).toMatch(/frequency of use/i);
+ });
+});
+
+describe('getRecentLayers', () => {
+ it('returns empty array when localStorage returns null', () => {
+ safeLocalStorage.getItem.mockReturnValue(null);
+ const result = getRecentLayers({}, PROJ);
+ expect(result).toEqual([]);
+ });
+
+ it('returns empty array when proj key does not exist in stored layers', () => {
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify({ arctic: [] }));
+ const result = getRecentLayers({}, PROJ);
+ expect(result).toEqual([]);
+ });
+
+ it('returns mapped layer objects sorted by count descending', () => {
+ const stored = makeStoredLayers({
+ geographic: [
+ makeLayerEntry('layer-a', 2, 500),
+ makeLayerEntry('layer-b', 5, 400),
+ makeLayerEntry('layer-c', 1, 600),
+ ],
+ });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ const layerConfig = {
+ 'layer-a': { id: 'layer-a', title: 'Layer A' },
+ 'layer-b': { id: 'layer-b', title: 'Layer B' },
+ 'layer-c': { id: 'layer-c', title: 'Layer C' },
+ };
+ const result = getRecentLayers(layerConfig, PROJ);
+ expect(result[0].id).toBe('layer-b');
+ expect(result[1].id).toBe('layer-a');
+ expect(result[2].id).toBe('layer-c');
+ });
+
+ it('filters out layers not found in layerConfig', () => {
+ const stored = makeStoredLayers({
+ geographic: [
+ makeLayerEntry('layer-known', 3, 500),
+ makeLayerEntry('layer-unknown', 3, 400),
+ ],
+ });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ const layerConfig = {
+ 'layer-known': { id: 'layer-known', title: 'Known' },
+ };
+ const result = getRecentLayers(layerConfig, PROJ);
+ expect(result).toHaveLength(1);
+ expect(result[0].id).toBe('layer-known');
+ });
+
+ it('sorts by dateAdded ascending when counts are equal', () => {
+ const stored = makeStoredLayers({
+ geographic: [
+ makeLayerEntry('layer-newer', 3, 800),
+ makeLayerEntry('layer-older', 3, 200),
+ ],
+ });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ const layerConfig = {
+ 'layer-newer': { id: 'layer-newer' },
+ 'layer-older': { id: 'layer-older' },
+ };
+ const result = getRecentLayers(layerConfig, PROJ);
+ expect(result[0].id).toBe('layer-older');
+ expect(result[1].id).toBe('layer-newer');
+ });
+});
+
+describe('clearRecentLayers', () => {
+ it('calls safeLocalStorage.removeItem with the correct key', () => {
+ clearRecentLayers();
+ expect(safeLocalStorage.removeItem).toHaveBeenCalledWith('RECENT_LAYERS');
+ });
+});
+
+describe('clearSingleRecentLayer', () => {
+ it('removes the layer from all its projections in storage', () => {
+ const stored = makeStoredLayers({
+ geographic: [makeLayerEntry('layer-1', 1, 100), makeLayerEntry('layer-2', 2, 200)],
+ arctic: [makeLayerEntry('layer-1', 1, 100)],
+ });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+
+ const layer = {
+ id: 'layer-1',
+ projections: { geographic: {}, arctic: {} },
+ };
+
+ clearSingleRecentLayer(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ expect(saved.geographic.find((l) => l.id === 'layer-1')).toBeUndefined();
+ expect(saved.arctic.find((l) => l.id === 'layer-1')).toBeUndefined();
+ expect(saved.geographic.find((l) => l.id === 'layer-2')).toBeDefined();
+ });
+
+ it('handles missing projection keys in storage gracefully', () => {
+ safeLocalStorage.getItem.mockReturnValue(null);
+
+ const layer = {
+ id: 'layer-1',
+ projections: { geographic: {} },
+ };
+
+ expect(() => clearSingleRecentLayer(layer, ALL_PROJECTIONS)).not.toThrow();
+ expect(safeLocalStorage.setItem).toHaveBeenCalled();
+ });
+});
+
+describe('updateRecentLayers', () => {
+ it('does nothing when safeLocalStorage is not enabled', () => {
+ safeLocalStorage.enabled = false;
+ const layer = { id: 'layer-1', projections: { geographic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+ expect(safeLocalStorage.setItem).not.toHaveBeenCalled();
+ });
+
+ it('adds a new layer entry with count 1', () => {
+ const stored = makeStoredLayers({ geographic: [] });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ util.now.mockReturnValue({ valueOf: () => 12345 });
+
+ const layer = { id: 'layer-new', projections: { geographic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ const entry = saved.geographic.find((l) => l.id === 'layer-new');
+ expect(entry).toBeDefined();
+ expect(entry.count).toBe(1);
+ expect(entry.dateAdded).toBe(12345);
+ });
+
+ it('increments count for an existing layer entry', () => {
+ const stored = makeStoredLayers({
+ geographic: [makeLayerEntry('layer-existing', 3, 500)],
+ });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ util.now.mockReturnValue({ valueOf: () => 9999 });
+
+ const layer = { id: 'layer-existing', projections: { geographic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ const entry = saved.geographic.find((l) => l.id === 'layer-existing');
+ expect(entry.count).toBe(4);
+ expect(entry.dateAdded).toBe(9999);
+ });
+
+ it('evicts the oldest lowest-count entry when at MAX (20) layers', () => {
+ const layers = Array.from({ length: 20 }, (_, i) => makeLayerEntry(`layer-${i}`, i + 1, (i + 1) * 100));
+ layers[0] = makeLayerEntry('layer-0', 1, 50);
+ layers[1] = makeLayerEntry('layer-1', 1, 100);
+
+ const stored = makeStoredLayers({ geographic: layers });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ util.now.mockReturnValue({ valueOf: () => 99999 });
+
+ const layer = { id: 'layer-new', projections: { geographic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ expect(saved.geographic).toHaveLength(20);
+ expect(saved.geographic.find((l) => l.id === 'layer-0')).toBeUndefined();
+ expect(saved.geographic.find((l) => l.id === 'layer-new')).toBeDefined();
+ });
+
+ it('handles null localStorage (initializes from default object) when adding new layer', () => {
+ safeLocalStorage.getItem.mockReturnValue(null);
+ util.now.mockReturnValue({ valueOf: () => 1 });
+
+ const layer = { id: 'layer-fresh', projections: { geographic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ expect(saved.geographic.find((l) => l.id === 'layer-fresh')).toBeDefined();
+ });
+
+ it('updates across multiple projections', () => {
+ const stored = makeStoredLayers({ geographic: [], arctic: [] });
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(stored));
+ util.now.mockReturnValue({ valueOf: () => 777 });
+
+ const layer = { id: 'multi-proj-layer', projections: { geographic: {}, arctic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ expect(saved.geographic.find((l) => l.id === 'multi-proj-layer')).toBeDefined();
+ expect(saved.arctic.find((l) => l.id === 'multi-proj-layer')).toBeDefined();
+ });
+
+ it('adds missing projection keys when stored object is incomplete', () => {
+ const incomplete = { geographic: [] };
+ safeLocalStorage.getItem.mockReturnValue(JSON.stringify(incomplete));
+ util.now.mockReturnValue({ valueOf: () => 1 });
+
+ const layer = { id: 'layer-x', projections: { geographic: {} } };
+ updateRecentLayers(layer, ALL_PROJECTIONS);
+
+ const saved = JSON.parse(safeLocalStorage.setItem.mock.calls[0][1]);
+ expect(saved.arctic).toBeDefined();
+ expect(saved.antarctic).toBeDefined();
+ });
});