Skip to content

Commit

Permalink
add a few client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buzcarter committed Dec 11, 2023
1 parent 0cdbc2c commit 922ebad
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ module.exports = {
// 'jest/globals': true,
// },
globals: {
beforeEach: 'readonly',
describe: 'readonly',
it: 'readonly',
expect: 'readonly',
it: 'readonly',
},
}],
};
1 change: 1 addition & 0 deletions .jest/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
},
},
rootDir: resolve(__dirname, '../'),
setupFiles: ['<rootDir>/.jest/jest.setup.cjs'],
setupFilesAfterEnv: [],
testMatch: [
'<rootDir>/src/**/__tests__/**/*.test.js',
Expand Down
1 change: 1 addition & 0 deletions .jest/jest.setup.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require( 'jest-localstorage-mock');
14 changes: 12 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recipes-nodejs",
"version": "1.0.3",
"version": "1.0.4",
"description": "NodeJS recipe publisher, converts markdown/text recipe files into well formatted HTML.",
"main": "build.js",
"type": "module",
Expand Down Expand Up @@ -32,6 +32,7 @@
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.29.0",
"jest": "^29.7.0",
"jest-localstorage-mock": "^2.4.26",
"nodemon": "^3.0.1"
},
"repository": {
Expand Down
97 changes: 97 additions & 0 deletions src/static/scripts/libs/__tests__/preferences.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
KeyNames,
updateKey,
getKey,
updateMRUList,
} from '../preferences';

describe('preferences', () => {
beforeEach(() => {
localStorage.clear();
});

describe('updateKey', () => {
it('should update the value of a key in the app data', () => {
const key = KeyNames.RECENT;
const value = ['recipe1', 'recipe2'];

updateKey(key, value);

const updatedValue = getKey(key);
expect(updatedValue).toEqual(value);
});
});

describe('getKey', () => {
it('should return the default value when `key` has not been set', () => {
const key = KeyNames.SEARCH;
const defaultValue = 'default';

const value = getKey(key, defaultValue);
expect(value).toEqual(defaultValue);
});

it('should return the default value if the key does not exist', () => {
const key = 'nonExistentKey';
const defaultValue = 'default';

const value = getKey(key, defaultValue);
expect(value).toEqual(defaultValue);
});
});

describe('updateMRUList', () => {
it('should add a value to the MRU list', () => {
const key = KeyNames.RECENT;
const maxLength = 3;
const value = 'recipe1';

updateMRUList(key, maxLength, value);

const updatedList = getKey(key);
expect(updatedList).toEqual([value]);
});

it('should place new values at top of the MRU list', () => {
const key = KeyNames.RECENT;
const maxLength = 3;
const value1 = 'recipe1';
const value2 = 'recipe2';

updateMRUList(key, maxLength, value1);
updateMRUList(key, maxLength, value2);

const updatedList = getKey(key);
expect(updatedList).toEqual([value2, value1]);
});

it('should move an existing value to the top of the MRU list', () => {
const key = KeyNames.RECENT;
const maxLength = 3;
const value1 = 'recipe1';
const value2 = 'recipe2';

updateMRUList(key, maxLength, value1);
updateMRUList(key, maxLength, value2);
updateMRUList(key, maxLength, value1);

const updatedList = getKey(key);
expect(updatedList).toEqual([value1, value2]);
});

it('should discard the last item if the MRU list exceeds the max length', () => {
const key = KeyNames.RECENT;
const maxLength = 2;
const value1 = 'recipe1';
const value2 = 'recipe2';
const value3 = 'recipe3';

updateMRUList(key, maxLength, value1);
updateMRUList(key, maxLength, value2);
updateMRUList(key, maxLength, value3);

const updatedList = getKey(key);
expect(updatedList).toEqual([value3, value2]);
});
});
});
20 changes: 20 additions & 0 deletions src/static/scripts/libs/__tests__/recentlyViewed.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { toDisplay } from '../recentlyViewed';

describe('recentlyViewed', () => {
describe('toDisplay', () => {
const tests = [
{ value: 'hello-world', expectedResult: 'Hello World' },
{ value: 'hello world', expectedResult: 'Hello World' },
{ value: ' HeLLo WoRLD ', expectedResult: 'Hello World' },
{ value: '', expectedResult: '' },
{ value: '---', expectedResult: '' },
];

tests.forEach((test) => {
it(`should convert "${test.value}" to "${test.expectedResult}"`, () => {
const result = toDisplay(test.value);
expect(result).toEqual(test.expectedResult);
});
});
});
});
21 changes: 21 additions & 0 deletions src/static/scripts/libs/__tests__/searchBox.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { scrub } from '../searchBox';

describe('searchBox', () => {
describe('scrub', () => {
const tests = [
{ value: 'Hello World!', expectedResult: 'helloworld' },
{ value: ' A very delicious pumpkin -- pie! From, :Hasbro (99) ', expectedResult: 'averydeliciouspumpkinpiefromhasbro99' },
{ value: ' Hello World ', expectedResult: 'helloworld' },
{ value: '12345', expectedResult: '12345' },
{ value: '', expectedResult: '' },
{ value: '---', expectedResult: '' },
];

tests.forEach((test) => {
it(`should scrub "${test.value}" to "${test.expectedResult}"`, () => {
const result = scrub(test.value);
expect(result).toEqual(test.expectedResult);
});
});
});
});
2 changes: 1 addition & 1 deletion src/static/scripts/libs/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const updateMRUList = (key, maxLength, value) => {
list.unshift(value);

if (list.length > maxLength) {
list = list.slice(0, maxLength - 1);
list = list.slice(0, maxLength);
}

updateKey(key, list);
Expand Down
6 changes: 4 additions & 2 deletions src/static/scripts/libs/recentlyViewed.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const Styles = {
MODAL_ACTIVE: 'modal--is-active',
};

const toDisplay = (value) => value
export const toDisplay = (value) => value
.replace(/-/g, ' ')
.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase())
.replace(/\s+/g, ' ')
.trim();

function onRecipeLinkClick(e) {
const anchor = e.target.tagName === 'A'
Expand Down
2 changes: 1 addition & 1 deletion src/static/scripts/libs/searchBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const KeyCodes = {
};
/* eslint-enable key-spacing */

const scrub = (value) => value
export const scrub = (value) => value
.trim()
.toLowerCase()
.replace(/\W/g, '')
Expand Down

0 comments on commit 922ebad

Please sign in to comment.