Skip to content

Commit

Permalink
fix: #739 cannot assign to readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuong Vu committed Apr 3, 2020
1 parent 8d3bd81 commit 35b23bb
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/aml-checklist/src/tests/badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion packages/cognito-auth/src/tests/badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 22 additions & 3 deletions packages/elements/src/components/DynamicLinks/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,29 @@ describe('restoreGlobalObjectFromLS', () => {
})

describe('injectSwitchModeToWindow', () => {
it('should call correctly', () => {
beforeAll(() => {
;(restoreGlobalObjectFromLS as jest.Mocked<any>) = jest.fn()
Object.defineProperty = jest.fn()
const spyDefineProperty = window.spyOn(Object, 'defineProperty')
})

afterAll(() => {
;(restoreGlobalObjectFromLS as jest.Mocked<any>).mockRestore()
})

afterEach(() => {
;(Object.defineProperty as jest.Mocked<any>).mockClear()
;(restoreGlobalObjectFromLS as jest.Mocked<any>).mockClear()
})
it('should call correctly', () => {
injectSwitchModeToWindow()
expect(Object.defineProperty).toHaveBeenCalledTimes(2)
expect(restoreGlobalObjectFromLS).toHaveBeenCalledTimes(1)
})

it('should not call restoreGlobalObjectFromLS if not writable or window[GLOBAL_KEY] is undefined', () => {
Object.getOwnPropertyDescriptor = jest.fn(() => ({ writable: false }))
injectSwitchModeToWindow()
expect(spyDefineProperty).toHaveBeenCalledTimes(2)
expect(Object.defineProperty).toHaveBeenCalledTimes(2)
expect(restoreGlobalObjectFromLS).not.toHaveBeenCalled()
})
})
8 changes: 6 additions & 2 deletions packages/elements/src/components/DynamicLinks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export const injectSwitchModeToWindow = () => {
Object.defineProperty(window, 'webMode', {
value: clearMarkeplaceGlobals,
})

restoreGlobalObjectFromLS()
// Only restore if window[GLOBAL_KEY] is writable
// In REAL DESKTOP mode, window.__REAPIT_MARKETPLACE_GLOBALS__ writable is false
// and this function will not be executed
if (Object.getOwnPropertyDescriptor(window, GLOBAL_KEY)?.writable) {
restoreGlobalObjectFromLS()
}
}
2 changes: 1 addition & 1 deletion packages/elements/src/tests/badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/marketplace/src/tests/badges/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 35b23bb

Please sign in to comment.