PluginProbe
Extendify / 3.1.1
Extendify v3.1.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / tests / unit / QuickEdit / entry-default-on.test.js

entry-default-on.test.js in Extendify 3.1.1, at tests/unit/QuickEdit/entry-default-on.test.js

111 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // Pins the cross-bundle race fix in quick-edit.jsx: when the Agent bundle
2 // imports `@quick-edit/state/edit-mode` and evaluates the shared chunk
3 // before our `'before'` inline script has set `window.extQuickEditData`,
4 // `DEFAULT_ON` in edit-mode.js resolves to false. By the time this entry
5 // bundle loads its own inline data is reliably set, so the IIFE re-seeds
6 // the store to honor the server's defaultOn unless the user has a
7 // persisted choice.
8
9 jest.mock('@wordpress/dom-ready', () => jest.fn());
10 jest.mock('@wordpress/element', () => {
11 const actual = jest.requireActual('@wordpress/element');
12 return {
13 ...actual,
14 createRoot: jest.fn(() => ({ render: jest.fn() })),
15 render: jest.fn(),
16 };
17 });
18 jest.mock('@wordpress/data', () => ({
19 dispatch: () => ({ updateSettings: jest.fn() }),
20 }));
21 jest.mock('zustand/middleware', () => ({
22 devtools: (fn) => fn,
23 persist: (config) => config,
24 }));
25 jest.mock('@quick-edit/lib/insights', () => ({ track: jest.fn() }));
26 jest.mock('@quick-edit/components/InlineEditor', () => ({
27 InlineEditor: () => null,
28 }));
29 jest.mock('@quick-edit/lib/admin-bar', () => ({
30 bindAdminBarToggle: jest.fn(),
31 }));
32 jest.mock('@quick-edit/lib/global-escape', () => ({
33 wireGlobalEscape: jest.fn(),
34 }));
35 jest.mock('@quick-edit/lib/hover-bar', () => ({
36 attach: jest.fn(),
37 detach: jest.fn(),
38 }));
39 jest.mock('@quick-edit/lib/keyboard-entry', () => ({
40 attachKeyboardEntry: jest.fn(),
41 detachKeyboardEntry: jest.fn(),
42 }));
43 jest.mock('@quick-edit/lib/keyboard-undo', () => ({
44 attachKeyboardUndo: jest.fn(),
45 detachKeyboardUndo: jest.fn(),
46 }));
47 jest.mock('@quick-edit/lib/link-suggestions', () => ({
48 fetchLinkSuggestions: jest.fn(),
49 }));
50
51 beforeEach(() => {
52 jest.resetModules();
53 document.documentElement.className = '';
54 localStorage.clear();
55 delete window.extQuickEditData;
56 });
57
58 const loadEntry = () => require('@quick-edit/quick-edit');
59 const getEditModeStore = () =>
60 require('@quick-edit/state/edit-mode').useEditModeStore;
61
62 describe('quick-edit entry — defaultOn re-seed', () => {
63 it('re-seeds on=true when extQuickEditData arrives after edit-mode.js evaluated', () => {
64 // Simulate the race: edit-mode.js evaluates first (no extQuickEditData
65 // yet), then the entry bundle's inline lands the data, then the entry
66 // evaluates. After the entry loads, on should be true.
67 const editMode = getEditModeStore();
68 expect(editMode.getState().on).toBe(false);
69
70 window.extQuickEditData = { defaultOn: true };
71 loadEntry();
72
73 expect(editMode.getState().on).toBe(true);
74 });
75
76 it('does not re-seed when extQuickEditData.defaultOn is false', () => {
77 const editMode = getEditModeStore();
78 window.extQuickEditData = { defaultOn: false };
79 loadEntry();
80
81 expect(editMode.getState().on).toBe(false);
82 });
83
84 it('does not override a persisted user choice (off) even when defaultOn is true', () => {
85 // User explicitly toggled off in a prior session — persist hydrates
86 // to false. The seed must not blow that away.
87 localStorage.setItem(
88 'extendify-quick-edit-mode',
89 JSON.stringify({ state: { on: false }, version: 0 }),
90 );
91 const editMode = getEditModeStore();
92 window.extQuickEditData = { defaultOn: true };
93 loadEntry();
94
95 expect(editMode.getState().on).toBe(false);
96 });
97
98 it('leaves on=true alone when both server and persist agree', () => {
99 localStorage.setItem(
100 'extendify-quick-edit-mode',
101 JSON.stringify({ state: { on: true }, version: 0 }),
102 );
103 window.extQuickEditData = { defaultOn: true };
104 const editMode = getEditModeStore();
105 editMode.setState({ on: true });
106 loadEntry();
107
108 expect(editMode.getState().on).toBe(true);
109 });
110 });
111