-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from buzcarter/feature/keep_most_recent_views_…
…and_searches Feature/keep most recent views and searches
- Loading branch information
Showing
21 changed files
with
413 additions
and
40 deletions.
There are no files selected for viewing
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
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 @@ | ||
require( 'jest-localstorage-mock'); |
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,5 +1,5 @@ | ||
{ | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true | ||
"source.fixAll.eslint": "explicit" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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]); | ||
}); | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); | ||
}); | ||
}); |
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,58 @@ | ||
import { KeyNames, getKey, updateMRUList } from './preferences.js'; | ||
|
||
const MAX_LIST_LENGTH = 50; | ||
|
||
/* eslint-disable key-spacing */ | ||
const Selectors = { | ||
RECIPE_LIST: '#recipe-list', | ||
|
||
RECENTLY_VIEWED_BTN: '#show-recents-btn', | ||
|
||
MODAL: '#show-recents-modal', | ||
MODAL_CONTENT: '#show-recents-content', | ||
MODAL_CLOST_BTN: '#show-recents-close-btn', | ||
}; | ||
/* eslint-enable key-spacing */ | ||
|
||
const Styles = { | ||
MODAL_ACTIVE: 'modal--is-active', | ||
}; | ||
|
||
export const toDisplay = (value) => value | ||
.replace(/-/g, ' ') | ||
.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' | ||
? e.target | ||
: e.target.closest('a'); | ||
|
||
if (anchor) { | ||
const value = anchor.getAttribute('href'); | ||
if (value) { | ||
updateMRUList(KeyNames.RECENT, MAX_LIST_LENGTH, value.replace(/^.*\/|\.html$/g, '')); | ||
} | ||
} | ||
} | ||
|
||
function toggleModal(isActive) { | ||
const modal = document.querySelector(Selectors.MODAL); | ||
modal.classList.toggle(Styles.MODAL_ACTIVE, isActive); | ||
modal.setAttribute('aria-hidden', !isActive); | ||
} | ||
|
||
function onViewBtnClick() { | ||
toggleModal(true); | ||
document.querySelector(Selectors.MODAL_CONTENT).innerHTML = getKey(KeyNames.RECENT) | ||
.map((value) => `<li><a href="${value}.html">${toDisplay(value)}</a></li>`) | ||
.join(''); | ||
} | ||
|
||
export function init() { | ||
document.querySelector(Selectors.RECENTLY_VIEWED_BTN).addEventListener('click', onViewBtnClick); | ||
// event delegation: record all recipe link clicks | ||
document.querySelector(Selectors.RECIPE_LIST).addEventListener('click', onRecipeLinkClick); | ||
document.querySelector(Selectors.MODAL_CLOST_BTN).addEventListener('click', () => toggleModal(false)); | ||
} |
Oops, something went wrong.