Skip to content

Commit

Permalink
test(linear-progress): Add Jasmine foundation tests (#5507)
Browse files Browse the repository at this point in the history
  • Loading branch information
allan-chen authored Jan 22, 2020
1 parent 6092f71 commit 1504962
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 269 deletions.
4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const istanbulInstrumenterLoader = {
/form-field\/.*$/,
/icon-button\/.*$/,
/line-ripple\/.*$/,
/linear-progress\/.*$/,
/list\/.*$/,
/menu\/.*$/,
/menu-surface\/.*$/,
Expand Down Expand Up @@ -130,7 +131,7 @@ const mochaConfig = {
'emitWarning': false,
'thresholds': {
statements: 95,
branches: 95,
branches: 92.5,
lines: 95,
functions: 95,
},
Expand Down Expand Up @@ -182,6 +183,7 @@ const jasmineConfig = {
'packages/!(mdc-form-field)/**/*',
'packages/!(mdc-icon-button)/**/*',
'packages/!(mdc-line-ripple)/**/*',
'packages/!(mdc-linear-progress)/**/*',
'packages/!(mdc-list)/**/*',
'packages/!(mdc-menu)/**/*',
'packages/!(mdc-menu-surface)/**/*',
Expand Down
252 changes: 252 additions & 0 deletions packages/mdc-linear-progress/test/foundation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/**
* @license
* Copyright 2020 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


import {MDCLinearProgressFoundation} from '../../mdc-linear-progress/foundation';
import {checkNumTimesSpyCalledWithArgs, verifyDefaultAdapter} from '../../../testing/helpers/foundation';
import {setUpFoundationTest} from '../../../testing/helpers/setup';

const {cssClasses, strings} = MDCLinearProgressFoundation;

describe('MDCLinearProgressFoundation', () => {
it('exports strings', () => {
expect('strings' in MDCLinearProgressFoundation).toBeTruthy();
});

it('exports cssClasses', () => {
expect('cssClasses' in MDCLinearProgressFoundation).toBeTruthy();
});

it('defaultAdapter returns a complete adapter implementation', () => {
verifyDefaultAdapter(MDCLinearProgressFoundation, [
'addClass',
'getPrimaryBar',
'forceLayout',
'getBuffer',
'hasClass',
'removeAttribute',
'removeClass',
'setAttribute',
'setStyle',
]);
});

const setupTest = () => {
const {foundation, mockAdapter} =
setUpFoundationTest(MDCLinearProgressFoundation);
return {foundation, mockAdapter};
};

it('#setDeterminate false adds class, resets transforms, and removes aria-valuenow',
() => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(false);
const primaryBar = {};
mockAdapter.getPrimaryBar.and.returnValue(primaryBar);
const buffer = {};
mockAdapter.getBuffer.and.returnValue(buffer);
foundation.init();
foundation.setDeterminate(false);
expect(mockAdapter.addClass)
.toHaveBeenCalledWith(cssClasses.INDETERMINATE_CLASS);
expect(mockAdapter.setStyle)
.toHaveBeenCalledWith(primaryBar, 'transform', 'scaleX(1)');
expect(mockAdapter.setStyle)
.toHaveBeenCalledWith(buffer, 'transform', 'scaleX(1)');
expect(mockAdapter.removeAttribute)
.toHaveBeenCalledWith(strings.ARIA_VALUENOW);
});

it('#setDeterminate removes class', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(false);
foundation.init();
foundation.setDeterminate(true);
expect(mockAdapter.removeClass)
.toHaveBeenCalledWith(cssClasses.INDETERMINATE_CLASS);
});

it('#setDeterminate false calls forceLayout to correctly reset animation timers when reversed',
() => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.REVERSED_CLASS)
.and.returnValue(true);
foundation.init();
foundation.setDeterminate(false);
expect(mockAdapter.forceLayout).toHaveBeenCalled();
});

it('#setDeterminate restores previous progress value after toggled from false to true',
() => {
const {foundation, mockAdapter} = setupTest();
const primaryBar = {};
mockAdapter.getPrimaryBar.and.returnValue(primaryBar);
foundation.init();
foundation.setProgress(0.123);
foundation.setDeterminate(false);
foundation.setDeterminate(true);

checkNumTimesSpyCalledWithArgs(
mockAdapter.setStyle, [primaryBar, 'transform', 'scaleX(0.123)'], 2);
checkNumTimesSpyCalledWithArgs(
mockAdapter.setAttribute, [strings.ARIA_VALUENOW, '0.123'], 2);
});

it('#setDeterminate restores previous buffer value after toggled from false to true',
() => {
const {foundation, mockAdapter} = setupTest();
const buffer = {};
mockAdapter.getBuffer.and.returnValue(buffer);
foundation.init();
foundation.setBuffer(0.123);
foundation.setDeterminate(false);
foundation.setDeterminate(true);
checkNumTimesSpyCalledWithArgs(
mockAdapter.setStyle, [buffer, 'transform', 'scaleX(0.123)'], 2);
});

it('#setDeterminate updates progress value set while determinate is false after determinate is true',
() => {
const {foundation, mockAdapter} = setupTest();
const primaryBar = {};
mockAdapter.getPrimaryBar.and.returnValue(primaryBar);
foundation.init();
foundation.setDeterminate(false);
foundation.setProgress(0.123);
foundation.setDeterminate(true);
expect(mockAdapter.setStyle)
.toHaveBeenCalledWith(primaryBar, 'transform', 'scaleX(0.123)');
expect(mockAdapter.setAttribute)
.toHaveBeenCalledWith(strings.ARIA_VALUENOW, '0.123');
});

it('#setProgress sets transform and aria-valuenow', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(false);
const primaryBar = {};
mockAdapter.getPrimaryBar.and.returnValue(primaryBar);
foundation.init();
foundation.setProgress(0.5);
expect(mockAdapter.setStyle)
.toHaveBeenCalledWith(primaryBar, 'transform', 'scaleX(0.5)');
expect(mockAdapter.setAttribute)
.toHaveBeenCalledWith(strings.ARIA_VALUENOW, '0.5');
});

it('#setProgress on indeterminate does nothing', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(true);
const primaryBar = {};
mockAdapter.getPrimaryBar.and.returnValue(primaryBar);
foundation.init();
foundation.setProgress(0.5);
expect(mockAdapter.setStyle).not.toHaveBeenCalled();
expect(mockAdapter.setAttribute).not.toHaveBeenCalled();
});

it('#setBuffer sets transform', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(false);
const buffer = {};
mockAdapter.getBuffer.and.returnValue(buffer);
foundation.init();
foundation.setBuffer(0.5);
expect(mockAdapter.setStyle)
.toHaveBeenCalledWith(buffer, 'transform', 'scaleX(0.5)');
});

it('#setBuffer on indeterminate does nothing', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(true);
const buffer = {};
mockAdapter.getBuffer.and.returnValue(buffer);
foundation.init();
foundation.setBuffer(0.5);
expect(mockAdapter.setStyle).not.toHaveBeenCalled();
});

it('#setReverse adds class', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.REVERSED_CLASS)
.and.returnValue(false);
foundation.init();
foundation.setReverse(true);
expect(mockAdapter.addClass)
.toHaveBeenCalledWith(cssClasses.REVERSED_CLASS);
});

it('#setReverse removes class', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.REVERSED_CLASS)
.and.returnValue(true);
foundation.init();
foundation.setReverse(false);
expect(mockAdapter.removeClass)
.toHaveBeenCalledWith(cssClasses.REVERSED_CLASS);
});

it('#setReverse true calls forceLayout to correctly reset animation timers when indeterminate',
() => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(true);
foundation.init();
foundation.setReverse(true);
expect(mockAdapter.forceLayout).toHaveBeenCalled();
});

it('#setReverse false calls forceLayout to correctly reset animation timers when indeterminate',
() => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
.and.returnValue(true);
foundation.init();
foundation.setReverse(false);
expect(mockAdapter.forceLayout).toHaveBeenCalled();
});

it('#open removes class', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.REVERSED_CLASS)
.and.returnValue(true);
foundation.init();
foundation.open();
expect(mockAdapter.removeClass)
.toHaveBeenCalledWith(cssClasses.CLOSED_CLASS);
});

it('#close adds class', () => {
const {foundation, mockAdapter} = setupTest();
mockAdapter.hasClass.withArgs(cssClasses.REVERSED_CLASS)
.and.returnValue(true);
foundation.init();
foundation.close();
expect(mockAdapter.addClass).toHaveBeenCalledWith(cssClasses.CLOSED_CLASS);
});
});
12 changes: 6 additions & 6 deletions packages/mdc-ripple/test/foundation-activation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

import {cssClasses, numbers, strings} from '../../mdc-ripple/constants';
import {MDCRippleFoundation} from '../../mdc-ripple/foundation';
import {captureHandlers} from '../../../testing/helpers/foundation';
import {captureHandlers, checkNumTimesSpyCalledWithArgs} from '../../../testing/helpers/foundation';
import {setUpMdcTestEnvironment} from '../../../testing/helpers/setup';
import {checkNumTimesSpyCalledWithStrArgs, setupTest, testFoundation} from './helpers';
import {setupTest, testFoundation} from './helpers';

describe('MDCRippleFoundation - Activation Logic', () => {
setUpMdcTestEnvironment();
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('MDCRippleFoundation - Activation Logic', () => {
jasmine.clock().tick(1);

handlers['keydown']();
checkNumTimesSpyCalledWithStrArgs(
checkNumTimesSpyCalledWithArgs(
adapter.addClass, [cssClasses.FG_ACTIVATION], 1);
});

Expand Down Expand Up @@ -325,7 +325,7 @@ describe('MDCRippleFoundation - Activation Logic', () => {
foundation.activate();
jasmine.clock().tick(1);

checkNumTimesSpyCalledWithStrArgs(
checkNumTimesSpyCalledWithArgs(
adapter.addClass, [cssClasses.FG_ACTIVATION], 2);
});

Expand Down Expand Up @@ -377,7 +377,7 @@ describe('MDCRippleFoundation - Activation Logic', () => {
handlers['mousedown']();
jasmine.clock().tick(1);

checkNumTimesSpyCalledWithStrArgs(
checkNumTimesSpyCalledWithArgs(
adapter.addClass, [cssClasses.FG_ACTIVATION], 1);
});

Expand All @@ -394,7 +394,7 @@ describe('MDCRippleFoundation - Activation Logic', () => {
handlers['pointerdown']();
jasmine.clock().tick(1);

checkNumTimesSpyCalledWithStrArgs(
checkNumTimesSpyCalledWithArgs(
adapter.addClass, [cssClasses.FG_ACTIVATION], 1);
});

Expand Down
Loading

0 comments on commit 1504962

Please sign in to comment.