Skip to content

Commit

Permalink
Adds a window.rsf_toggleLoading function that allows developers to ea…
Browse files Browse the repository at this point in the history
…sily toggle skeletons using chrome developer tools. (#65)
  • Loading branch information
markbrocato authored Mar 26, 2020
1 parent 6be4649 commit cf9a131
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-storefront",
"version": "7.9.2",
"version": "7.10.0",
"description": "Build and deploy e-commerce progressive web apps (PWAs) in record time.",
"module": "./index.js",
"license": "Apache-2.0",
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useLazyState.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ export default function useLazyState(lazyProps, additionalData = {}) {

useEffect(() => {
isInitialMount.current = false

if (process.env.NODE_ENV !== 'production') {
// expose a global function that makes it easy to toggle skeletons during development via chrome develeoper console
window.rsf_toggleLoading = () =>
updateState(state => ({
...state,
loading: !state.loading,
}))

return () => delete window.rsf_toggleLoading
}
}, [])

// save the page state in history.state before navigation
Expand Down
2 changes: 0 additions & 2 deletions test/carousel/MediaCarousel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,13 @@ describe('MediaCarousel', () => {
it('should hide the magnify hint if the lightbox is opened and the window is wider than the image', () => {
wrapper = mount(<MediaCarousel media={media} />)
wrapper.find(Carousel).simulate('click')
console.log(wrapper.find(MagnifyHint))
expect(wrapper.find(MagnifyHint)).toExist()

window.innerWidth = 1300
window.dispatchEvent(new Event('resize'))

wrapper = mount(<MediaCarousel media={media} />)
wrapper.find(Carousel).simulate('click')
console.log(wrapper.find(MagnifyHint))
expect(wrapper.find(MagnifyHint)).not.toExist()
})

Expand Down
26 changes: 26 additions & 0 deletions test/hooks/useLazyState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,30 @@ describe('useLazyState', () => {
value: 1,
})
})

it('should expose rsf_toggleLoading', () => {
wrapper = mount(<Test value={1} />)
act(() => window.rsf_toggleLoading())
expect(state.loading).toBe(true)
wrapper.unmount()
expect(window.rsf_toggleLoading).not.toBeDefined()
})

describe('production', () => {
let { NODE_ENV } = process.env

beforeAll(() => {
process.env.NODE_ENV = 'production'
})

afterAll(() => {
process.env.NODE_ENV = NODE_ENV
})

it('should not expose rsf_toggleLoading in production', () => {
wrapper = mount(<Test value={1} />)
act(() => {})
expect(window.rsf_toggleLoading).not.toBeDefined()
})
})
})
24 changes: 21 additions & 3 deletions test/nav/NavTab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,25 @@ describe('NavTab', () => {
.find(Link)
.first()
.simulate('keydown', { key: 'Enter' })
setImmediate(() => wrapper.update())

return new Promise(resolve => {
setTimeout(() => {
wrapper.update()
resolve()
})
})
})
expect(wrapper.find(Popover).prop('open')).toBe(true)

await act(async () => {
await navigate()
setImmediate(() => wrapper.update())

return new Promise(resolve => {
setTimeout(() => {
wrapper.update()
resolve()
})
})
})

expect(wrapper.find(Popover).prop('open')).toBe(false)
Expand Down Expand Up @@ -249,7 +261,13 @@ describe('NavTab', () => {
.find('a')
.first()
.simulate('blur')
setImmediate(() => wrapper.update())

return new Promise(resolve => {
setTimeout(() => {
wrapper.update()
resolve()
}, 1)
})
})

expect(wrapper.find(Popover).prop('open')).toBe(false)
Expand Down

0 comments on commit cf9a131

Please sign in to comment.