Skip to content

Commit 3966de8

Browse files
committed
test(core): adding test for hardware back button fallback
1 parent ecdbcee commit 3966de8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

core/src/utils/test/hardware-back-button.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,47 @@ describe('Hardware Back Button', () => {
5454
dispatchBackButtonEvent();
5555
expect(cbSpyTwo).toHaveBeenCalled();
5656
});
57+
58+
it('should fall back to history.back() when no handlers are registered', () => {
59+
const historyBackSpy = jest.fn();
60+
const originalBack = win?.history?.back;
61+
if (win?.history) {
62+
win.history.back = historyBackSpy;
63+
}
64+
65+
// Don't register any ionBackButton handlers
66+
dispatchBackButtonEvent();
67+
68+
expect(historyBackSpy).toHaveBeenCalled();
69+
70+
// Restore original
71+
if (win?.history && originalBack) {
72+
win.history.back = originalBack;
73+
}
74+
});
75+
76+
it('should not call history.back() when a handler is registered', () => {
77+
const historyBackSpy = jest.fn();
78+
const originalBack = win?.history?.back;
79+
if (win?.history) {
80+
win.history.back = historyBackSpy;
81+
}
82+
83+
const cbSpy = jest.fn();
84+
document.addEventListener('ionBackButton', (ev) => {
85+
(ev as BackButtonEvent).detail.register(0, cbSpy);
86+
});
87+
88+
dispatchBackButtonEvent();
89+
90+
expect(cbSpy).toHaveBeenCalled();
91+
expect(historyBackSpy).not.toHaveBeenCalled();
92+
93+
// Restore original
94+
if (win?.history && originalBack) {
95+
win.history.back = originalBack;
96+
}
97+
});
5798
});
5899

59100
describe('Experimental Close Watcher', () => {

0 commit comments

Comments
 (0)