Skip to content

Commit 64c0db0

Browse files
committed
test(app-shell): install the native-confirm spy by property descriptor, no any-casts
The pin now saves the window's own `confirm` descriptor, defines the spy over it, and hands the original back (or removes the own slot) after each test. Claude-Session: https://claude.ai/code/session_011p7ikEivgXefNDaE5S5Uec Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2a4a809 commit 64c0db0

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

‎packages/app-shell/src/views/RecordDetailView.deleteConfirmDialog-11001.test.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,15 @@ async function chooseHeaderDelete() {
177177
}
178178

179179
let nativeConfirm: ReturnType<typeof vi.fn>;
180-
let hadNativeConfirm = false;
181-
let originalNativeConfirm: unknown;
180+
/** The window's own `confirm` slot as it was, so the file hands it back. */
181+
let ownConfirmBefore: PropertyDescriptor | undefined;
182182

183183
beforeEach(() => {
184184
cleanup();
185185
// A spy that answers YES: the native-box code would delete on the spot.
186-
hadNativeConfirm = Object.prototype.hasOwnProperty.call(window, 'confirm');
187-
originalNativeConfirm = (window as any).confirm;
186+
ownConfirmBefore = Object.getOwnPropertyDescriptor(window, 'confirm');
188187
nativeConfirm = vi.fn(() => true);
189-
(window as any).confirm = nativeConfirm;
188+
Object.defineProperty(window, 'confirm', { value: nativeConfirm, configurable: true, writable: true });
190189
// Unrelated chrome (approvals, favourites, the record-explain probe) reaches
191190
// for the platform API; answer locally so the record read is the only
192191
// asynchrony before the header renders.
@@ -202,8 +201,8 @@ beforeEach(() => {
202201
});
203202

204203
afterEach(() => {
205-
if (hadNativeConfirm) (window as any).confirm = originalNativeConfirm;
206-
else delete (window as any).confirm;
204+
if (ownConfirmBefore) Object.defineProperty(window, 'confirm', ownConfirmBefore);
205+
else Reflect.deleteProperty(window, 'confirm');
207206
vi.unstubAllGlobals();
208207
vi.clearAllMocks();
209208
});

0 commit comments

Comments
 (0)