Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 8d9a549

Browse files
authored
Merge pull request #97 from material-components/sgomes-improve-drawer-coverage
test(drawer): Add more tests to improve branch coverage.
2 parents 05e0176 + 4dfae4c commit 8d9a549

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

test/unit/mdc-drawer/temporary.foundation.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,33 @@ test('on touch move does not allow the drawer to move past its width', (t) => {
365365
t.end();
366366
});
367367

368+
test('on touch move does not allow the drawer to move past its width in RTL', (t) => {
369+
const {foundation, mockAdapter} = setupTest();
370+
const drawerHandlers = captureHandlers(mockAdapter, 'registerDrawerInteractionHandler');
371+
const handlers = captureHandlers(mockAdapter, 'registerInteractionHandler');
372+
const raf = createMockRaf();
373+
td.when(mockAdapter.hasClass('mdc-temporary-drawer--open')).thenReturn(true);
374+
td.when(mockAdapter.getDrawerWidth()).thenReturn(500);
375+
td.when(mockAdapter.isRtl()).thenReturn(true);
376+
foundation.init();
377+
378+
drawerHandlers.touchstart({
379+
touches: [{pageX: 500}],
380+
});
381+
raf.flush();
382+
383+
handlers.touchmove({
384+
touches: [{pageX: 490}],
385+
preventDefault: () => {},
386+
});
387+
raf.flush();
388+
389+
t.doesNotThrow(() => td.verify(mockAdapter.setTranslateX(0)));
390+
t.doesNotThrow(() => td.verify(mockAdapter.updateCssVariable(1)));
391+
raf.restore();
392+
t.end();
393+
});
394+
368395
test('on touch move works for pointer events', (t) => {
369396
const {foundation, mockAdapter} = setupTest();
370397
const drawerHandlers = captureHandlers(mockAdapter, 'registerDrawerInteractionHandler');
@@ -491,6 +518,58 @@ test('on touch end keeps the drawer open if moved less than 50%', (t) => {
491518
t.end();
492519
});
493520

521+
test('on touch end closes the drawer if moved more than 50% in RTL', (t) => {
522+
const {foundation, mockAdapter} = setupTest();
523+
const drawerHandlers = captureHandlers(mockAdapter, 'registerDrawerInteractionHandler');
524+
const handlers = captureHandlers(mockAdapter, 'registerInteractionHandler');
525+
const raf = createMockRaf();
526+
td.when(mockAdapter.hasClass('mdc-temporary-drawer--open')).thenReturn(true);
527+
td.when(mockAdapter.getDrawerWidth()).thenReturn(500);
528+
td.when(mockAdapter.isRtl()).thenReturn(true);
529+
foundation.init();
530+
531+
drawerHandlers.touchstart({
532+
touches: [{pageX: 100}],
533+
});
534+
raf.flush();
535+
536+
handlers.touchmove({
537+
touches: [{pageX: 500}],
538+
preventDefault: () => {},
539+
});
540+
541+
handlers.touchend({});
542+
t.doesNotThrow(() => td.verify(mockAdapter.removeClass('mdc-temporary-drawer--open')));
543+
raf.restore();
544+
t.end();
545+
});
546+
547+
test('on touch end keeps the drawer open if moved less than 50% in RTL', (t) => {
548+
const {foundation, mockAdapter} = setupTest();
549+
const drawerHandlers = captureHandlers(mockAdapter, 'registerDrawerInteractionHandler');
550+
const handlers = captureHandlers(mockAdapter, 'registerInteractionHandler');
551+
const raf = createMockRaf();
552+
td.when(mockAdapter.hasClass('mdc-temporary-drawer--open')).thenReturn(true);
553+
td.when(mockAdapter.getDrawerWidth()).thenReturn(500);
554+
td.when(mockAdapter.isRtl()).thenReturn(true);
555+
foundation.init();
556+
557+
drawerHandlers.touchstart({
558+
touches: [{pageX: 300}],
559+
});
560+
raf.flush();
561+
562+
handlers.touchmove({
563+
touches: [{pageX: 500}],
564+
preventDefault: () => {},
565+
});
566+
567+
handlers.touchend({});
568+
t.doesNotThrow(() => td.verify(mockAdapter.addClass('mdc-temporary-drawer--open')));
569+
raf.restore();
570+
t.end();
571+
});
572+
494573
test('on touch end works with pointer events', (t) => {
495574
const {foundation, mockAdapter} = setupTest();
496575
const drawerHandlers = captureHandlers(mockAdapter, 'registerDrawerInteractionHandler');
@@ -625,6 +704,15 @@ test('on document keydown does nothing if drawer is not opened', (t) => {
625704
t.end();
626705
});
627706

707+
test('should clean up transition handlers after drawer close', (t) => {
708+
const {foundation, mockAdapter} = setupTest();
709+
td.when(mockAdapter.isDrawer(td.matchers.isA(Object))).thenReturn(true);
710+
td.when(mockAdapter.registerTransitionEndHandler(td.callback)).thenCallback({target: {}});
711+
foundation.close();
712+
t.doesNotThrow(() => td.verify(mockAdapter.deregisterTransitionEndHandler(td.matchers.isA(Function))));
713+
t.end();
714+
});
715+
628716
test('should not trigger bug #67', (t) => {
629717
const {foundation, mockAdapter} = setupTest();
630718
td.when(mockAdapter.isDrawer(td.matchers.isA(Object))).thenReturn(false);

0 commit comments

Comments
 (0)