| 1 |
// The `extendify-quick-edit-on` html class must never be applied inside |
| 2 |
// another tool's iframe. edit-mode.js ships in both the QuickEdit and Agent |
| 3 |
// bundles, so guarding the class here is what keeps the Customizer preview |
| 4 |
// (and page-builder previews) from looking edit-on even when the QE bundle |
| 5 |
// itself is gone. |
| 6 |
|
| 7 |
jest.mock('zustand/middleware', () => ({ |
| 8 |
devtools: (fn) => fn, |
| 9 |
persist: (config) => config, |
| 10 |
})); |
| 11 |
jest.mock('@quick-edit/lib/insights', () => ({ track: jest.fn() })); |
| 12 |
jest.mock('@shared/lib/embedded-guard', () => ({ isEmbedded: jest.fn() })); |
| 13 |
|
| 14 |
const HTML_CLASS = 'extendify-quick-edit-on'; |
| 15 |
|
| 16 |
const loadStore = async ({ embedded, defaultOn }) => { |
| 17 |
require('@shared/lib/embedded-guard').isEmbedded.mockReturnValue(embedded); |
| 18 |
window.extQuickEditData = defaultOn ? { defaultOn: true } : undefined; |
| 19 |
const mod = await import('@quick-edit/state/edit-mode'); |
| 20 |
return mod.useEditModeStore; |
| 21 |
}; |
| 22 |
|
| 23 |
beforeEach(() => { |
| 24 |
jest.resetModules(); |
| 25 |
jest.clearAllMocks(); |
| 26 |
document.documentElement.className = ''; |
| 27 |
window.extQuickEditData = undefined; |
| 28 |
}); |
| 29 |
|
| 30 |
describe('edit-mode: extendify-quick-edit-on guarded by isEmbedded', () => { |
| 31 |
it('applies the on-class at import on a top-level render', async () => { |
| 32 |
await loadStore({ embedded: false, defaultOn: true }); |
| 33 |
expect(document.documentElement.classList.contains(HTML_CLASS)).toBe(true); |
| 34 |
}); |
| 35 |
|
| 36 |
it('does NOT apply the on-class at import when embedded', async () => { |
| 37 |
await loadStore({ embedded: true, defaultOn: true }); |
| 38 |
expect(document.documentElement.classList.contains(HTML_CLASS)).toBe(false); |
| 39 |
}); |
| 40 |
|
| 41 |
it('keeps the on-class off when an embedded state change turns edit mode on', async () => { |
| 42 |
const useEditModeStore = await loadStore({ |
| 43 |
embedded: true, |
| 44 |
defaultOn: false, |
| 45 |
}); |
| 46 |
useEditModeStore.getState().setOn(true); |
| 47 |
expect(document.documentElement.classList.contains(HTML_CLASS)).toBe(false); |
| 48 |
}); |
| 49 |
|
| 50 |
it('applies the on-class on a top-level state change (control)', async () => { |
| 51 |
const useEditModeStore = await loadStore({ |
| 52 |
embedded: false, |
| 53 |
defaultOn: false, |
| 54 |
}); |
| 55 |
useEditModeStore.getState().setOn(true); |
| 56 |
expect(document.documentElement.classList.contains(HTML_CLASS)).toBe(true); |
| 57 |
}); |
| 58 |
}); |
| 59 |
|