Skip to content

Commit

Permalink
test(switch): add basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion committed Jan 10, 2016
1 parent 86c6bf8 commit 08f0ca4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
14 changes: 12 additions & 2 deletions karma-test-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ __karma__.loaded = function() {};
/**
* Gets map of module alias to location or package.
* @param dir Directory name under `src/` for create a map for.
* @param core Is that a map of the core files
*/
function getPathsMap(dir) {
function getPathsMap(dir, core) {
return Object.keys(window.__karma__.files)
.filter(isComponentsFile)
.filter(!!core ? isCoreFile : isComponentsFile)
.reduce(function(pathsMapping, appPath) {
var pathToReplace = new RegExp('^/base/dist/' + dir + '/');
var moduleName = appPath.replace(pathToReplace, './').replace(/\.js$/, '');
Expand All @@ -26,6 +27,11 @@ System.config({
defaultExtension: false,
format: 'register',
map: getPathsMap('components')
},
'base/dist/core': {
defaultExtension: false,
format: 'register',
map: getPathsMap('core', true)
}
}
});
Expand All @@ -46,6 +52,10 @@ System.import('angular2/platform/browser').then(function(browser_adapter) {
__karma__.error(error.stack || error);
});

function isCoreFile(filePath) {
return /^\/base\/dist\/core\/(?!spec)([a-z0-9-_\/]+)\.js$/.test(filePath);
}

function isComponentsFile(filePath) {
return /^\/base\/dist\/components\/(?!spec)([a-z0-9-_\/]+)\.js$/.test(filePath);
}
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function(config) {
proxies: {
// required for component assests fetched by Angular's compiler
"/demo-app/": "/base/dist/demo-app/",
"/components/": "/base/dist/components/",
"/components/": "/base/dist/components/"
},
exclude: [],
preprocessors: {},
Expand Down
59 changes: 59 additions & 0 deletions src/components/switch/switch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
it,
iit,
describe,
ddescribe,
expect,
inject,
injectAsync,
TestComponentBuilder,
beforeEachProviders,
beforeEach,
} from 'angular2/testing';
import {provide, Component} from 'angular2/core';
import {DebugElement} from "angular2/core";
import {MdSwitch} from './switch';
import {AsyncTestFn} from "angular2/testing";
import {FORM_DIRECTIVES} from "angular2/common";
import {Input} from "angular2/core";


describe('MdSwitch', () => {
let builder: TestComponentBuilder;

beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { builder = tcb; }));

describe('md-switch', () => {
it('should change the model value', (done: () => void) => {
return builder.createAsync(TestApp).then((fixture) => {
let testComponent = fixture.debugElement.componentInstance;
let switchElement = getChildDebugElement(fixture.debugElement, 'md-switch');

expect(switchElement.nativeElement.classList.contains('md-checked')).toBe(false);

testComponent.testSwitch = true;

fixture.detectChanges();

expect(switchElement.nativeElement.classList.contains('md-checked')).toBe(true);
done();
});
});
});
});

/** Gets a child DebugElement by tag name. */
function getChildDebugElement(parent: DebugElement, tagName: string): DebugElement {
return parent.query(debugEl => debugEl.nativeElement.tagName.toLowerCase() == tagName);
}

/** Test component that contains an MdSwitch. */
@Component({
selector: 'test-app',
directives: [MdSwitch, FORM_DIRECTIVES],
template:
'<md-switch [(ngModel)]="testSwitch">Test Switch</md-switch>',
})
class TestApp {
testSwitch = false;
}
1 change: 0 additions & 1 deletion src/core/services/drag/drag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {DOM} from "angular2/src/platform/dom/dom_adapter";
import {Json} from "angular2/src/facade/lang";

export class MdDrag {

Expand Down

0 comments on commit 08f0ca4

Please sign in to comment.