Skip to content

Commit

Permalink
fix(core): avoid duplicate mediaQuery activations (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaerusKaru authored and ThomasBurleson committed Dec 18, 2018
1 parent 4be5cef commit 23592ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/lib/core/media-marshaller/media-marshaller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('media-marshaller', () => {
providers: [MockMatchMediaProvider]
});
spyOn(MediaMarshaller.prototype, 'activate').and.callThrough();
spyOn(MediaMarshaller.prototype, 'updateStyles').and.callThrough();
});

// Single async inject to save references; which are used in all tests below
Expand All @@ -39,6 +40,12 @@ describe('media-marshaller', () => {
expect(mediaMarshaller.activate).toHaveBeenCalled();
});

it('doesn\'t activate when match-media activates the same breakpoint twice', () => {
matchMedia.activate('xs');
matchMedia.activate('xs');
expect(mediaMarshaller.updateStyles).toHaveBeenCalledTimes(1);
});

it('should set correct activated breakpoint', () => {
matchMedia.activate('lg');
expect(mediaMarshaller.activatedBreakpoint).toBe('lg');
Expand Down
17 changes: 10 additions & 7 deletions src/lib/core/media-marshaller/media-marshaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ export class MediaMarshaller {
*/
activate(mc: MediaChange) {
const bp: BreakPoint | null = this.findByQuery(mc.mediaQuery);
if (mc.matches && bp) {
this.activatedBreakpoints.push(bp);
this.activatedBreakpoints.sort(prioritySort);
} else if (!mc.matches && bp) {
// Remove the breakpoint when it's deactivated
this.activatedBreakpoints.splice(this.activatedBreakpoints.indexOf(bp), 1);
if (bp) {
if (mc.matches && this.activatedBreakpoints.indexOf(bp) === -1) {
this.activatedBreakpoints.push(bp);
this.activatedBreakpoints.sort(prioritySort);
this.updateStyles();
} else if (!mc.matches && this.activatedBreakpoints.indexOf(bp) !== -1) {
// Remove the breakpoint when it's deactivated
this.activatedBreakpoints.splice(this.activatedBreakpoints.indexOf(bp), 1);
this.updateStyles();
}
}
this.updateStyles();
}

/**
Expand Down

0 comments on commit 23592ee

Please sign in to comment.