-
Notifications
You must be signed in to change notification settings - Fork 116
fix: loosing data when moving between steps on the checkout #440
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
Merged
Frodigo
merged 9 commits into
develop
from
bugfix/m2-53-loosing-data-when-moving-between-steps-on-the-checkout
Jan 24, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9965c9d
fix(theme): prevent user to click on next steps in the checkout
KevinGorjan f3e1809
feat(theme): added an asyncLocalStorage helper
KevinGorjan 5f654da
fix(theme): added feature so the User Account is filled in
KevinGorjan 6a95127
fix(theme): user can't go pages directly
KevinGorjan ffda57a
chore: merge develop into current branch
KevinGorjan 5cabb57
chore: merge develop into current branch
KevinGorjan 768dfbe
feat(theme): a helpers for validating is the user has access to a spe…
KevinGorjan c34b6a7
feat(theme): implemented the validation in the checkout steps
KevinGorjan 635114e
test: added tests for asyncLocalStorage and the steps helper
KevinGorjan 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
70 changes: 70 additions & 0 deletions
70
packages/theme/helpers/__tests__/asyncLocalStorage.spec.js
This file contains hidden or 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,70 @@ | ||
| import { setItem, getItem, mergeItem, removeItem, clear } from '~/helpers/asyncLocalStorage'; | ||
|
|
||
| describe('asyncLocalStorage :: Promised Based Localstorage Management', () => { | ||
| test('setItem :: should store to localStorage by key', async () => { | ||
| const KEY = 'jest' | ||
| const VSF_KEY = `vsf-${KEY}` | ||
| const VALUE = 'jest-localstorage-value'; | ||
| const VSF_VALUE = JSON.stringify(VALUE); | ||
|
|
||
| await setItem(KEY, VALUE); | ||
|
|
||
| expect(localStorage.setItem).toHaveBeenLastCalledWith(VSF_KEY, VSF_VALUE); | ||
| expect(localStorage.__STORE__[VSF_KEY]).toBe(VSF_VALUE); | ||
| expect(Object.keys(localStorage.__STORE__).length).toBe(1); | ||
| }); | ||
|
|
||
| test('getItem :: should get value from localStorage by key', async () => { | ||
| const KEY = 'jest' | ||
| const VSF_KEY = `vsf-${KEY}` | ||
| const VSF_VALUE = 'jest-localstorage-value'; | ||
|
|
||
| const VALUE = await getItem(KEY); | ||
|
|
||
| expect(localStorage.getItem).toHaveBeenLastCalledWith(VSF_KEY); | ||
| expect(localStorage.__STORE__[VSF_KEY]).toBe(JSON.stringify(VALUE)); | ||
| expect(VSF_VALUE).toBe(VALUE); | ||
| }); | ||
|
|
||
| test('merge :: should merge values to localStorage by key', async () => { | ||
| const KEY = 'jest' | ||
| const VSF_KEY = `vsf-${KEY}` | ||
| const VALUE = {row1: 'Lonely'}; | ||
| const VSF_VALUE = JSON.stringify(VALUE); | ||
| const MERGE_VALUE = {row2: 'Not anymore'}; | ||
| const VSF_MERGE_VALUE = JSON.stringify({ | ||
| row1: 'Lonely', | ||
| row2: 'Not anymore' | ||
| }) | ||
|
|
||
| await setItem(KEY, VALUE); | ||
|
|
||
| expect(localStorage.__STORE__[VSF_KEY]).toBe(VSF_VALUE); | ||
| expect(Object.keys(localStorage.__STORE__).length).toBe(1); | ||
|
|
||
| await mergeItem(KEY, MERGE_VALUE); | ||
|
|
||
| expect(localStorage.__STORE__[VSF_KEY]).toBe(VSF_MERGE_VALUE); | ||
| expect(Object.keys(localStorage.__STORE__).length).toBe(1); | ||
| }); | ||
|
|
||
| test('remove :: should remove a key and value from localStorage', async () => { | ||
| const KEY = 'jest' | ||
| const VSF_KEY = `vsf-${KEY}` | ||
|
|
||
| expect(Object.keys(localStorage.__STORE__).length).toBe(1); | ||
|
|
||
| await removeItem(KEY); | ||
|
|
||
| expect(localStorage.removeItem).toHaveBeenCalledWith(VSF_KEY); | ||
| expect(Object.keys(localStorage.__STORE__).length).toBe(0); | ||
| }); | ||
|
|
||
| test('clear :: should clear all values and keys from localStorage', async () => { | ||
| await clear(); | ||
|
|
||
| expect(localStorage.clear).toHaveBeenCalledWith(); | ||
| expect(Object.keys(localStorage.__STORE__).length).toBe(0); | ||
| }); | ||
| }); | ||
|
|
This file contains hidden or 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,42 @@ | ||
| const getVsfKey = (key) => `vsf-${key}`; | ||
|
|
||
| const mergeLocalStorageItem = (key: string, value: string) => { | ||
| const oldValue = window.localStorage.getItem(key); | ||
| const oldObject = JSON.parse(oldValue); | ||
| const newObject = value; | ||
| const nextValue = JSON.stringify({ ...JSON.parse(JSON.stringify(oldObject)), ...JSON.parse(JSON.stringify(newObject)) }); | ||
| window.localStorage.setItem(key, nextValue); | ||
| }; | ||
|
|
||
| const createPromise = (getValue, callback): Promise<any> => new Promise((resolve, reject) => { | ||
| try { | ||
| const value = getValue(); | ||
| if (callback) { | ||
| callback(null, value); | ||
| } | ||
| resolve(value); | ||
| } catch (err) { | ||
| if (callback) { | ||
| callback(err); | ||
| } | ||
| reject(err); | ||
| } | ||
| }); | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| export const getItem = (key: string, callback?: Function): Promise<any> => createPromise(() => JSON.parse(window.localStorage.getItem(getVsfKey(key))), callback); | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| export const setItem = (key: string, value: string, callback?: Function): Promise<any> => createPromise(() => (window.localStorage.setItem(getVsfKey(key), JSON.stringify(value))), callback); | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| export const removeItem = (key: string, callback?: Function): Promise<any> => createPromise(() => { | ||
| window.localStorage.removeItem(getVsfKey(key)); | ||
| }, callback); | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| export const mergeItem = (key: string, value: string, callback?: Function): Promise<any> => createPromise(() => mergeLocalStorageItem(getVsfKey(key), value), callback); | ||
|
|
||
| export const clear = (callback?: Function): Promise<any> => createPromise(() => { | ||
| window.localStorage.clear(); | ||
| }, callback); |
This file contains hidden or 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,26 @@ | ||
| import { isPreviousStepValid } from '~/helpers/checkout/steps' | ||
|
|
||
| const CHECKOUT_DATA = { | ||
| 'my-account': {a: 'b', c: 'd'} | ||
| } | ||
|
|
||
| describe('steps :: steps helper for the checkout', () => { | ||
|
|
||
| beforeEach(async () => { | ||
| localStorage.clear(); | ||
|
|
||
| localStorage.setItem('vsf-checkout', JSON.stringify(CHECKOUT_DATA)) | ||
| }); | ||
|
|
||
| test('user can continue', async () => { | ||
| const isValid = await isPreviousStepValid('my-account') | ||
|
|
||
| expect(isValid).toBeTruthy(); | ||
| }); | ||
|
|
||
| test('user can\'t continue', async () => { | ||
| const isValid = await isPreviousStepValid('billing') | ||
|
|
||
| expect(isValid).toBeFalsy(); | ||
| }); | ||
| }); |
This file contains hidden or 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,6 @@ | ||
| import { getItem } from '~/helpers/asyncLocalStorage'; | ||
|
|
||
| export const isPreviousStepValid = async (stepToValidate: string) => { | ||
| const checkout = await getItem('checkout'); | ||
| return !(!checkout || !checkout[stepToValidate]); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.