Skip to content

Commit 83ccc56

Browse files
author
Wallace Breza
committed
Added unit tests for javascript components
1 parent a652088 commit 83ccc56

10 files changed

Lines changed: 250 additions & 31 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
dist/
2-
node_modules/
2+
node_modules/
3+
bower_components/
4+
coverage/

‎bower.json‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "powerbi-javascript",
3+
"description": "",
4+
"main": "./dist/powerbi.min.js",
5+
"authors": [
6+
"Microsoft"
7+
],
8+
"license": "MIT",
9+
"moduleType": [],
10+
"homepage": "https://dev.powerbi.com",
11+
"private": true,
12+
"ignore": [
13+
"**/.*",
14+
"node_modules",
15+
"bower_components",
16+
"src",
17+
"test",
18+
"tests"
19+
],
20+
"devDependencies": {
21+
"jquery": "~2.2.0"
22+
}
23+
}

‎gulpfile.js‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@ var rename = require('gulp-rename'),
33
concat = require('gulp-concat'),
44
uglify = require('gulp-uglify'),
55
jshint = require('gulp-jshint'),
6-
rimraf = require('rimraf');
7-
8-
var paths = {
9-
outFilename: 'powerbi.js',
10-
outMinFilename: 'powerbi.min.js',
11-
jsSource: './src/**/*.js',
12-
jsDest: './dist/'
13-
};
6+
rimraf = require('rimraf'),
7+
karma = require('karma'),
8+
paths = require('./paths');
149

1510
gulp.task('watch', 'Watches for changes', ['lint'], function () {
16-
gulp.watch(paths.jsSource, ['lint']);
11+
gulp.watch(paths.jsSource, ['lint:js']);
12+
gulp.watch(paths.specSource, ['lint:spec', 'test']);
1713
});
1814

19-
gulp.task('lint', 'Lints all files', ['lint:js']);
20-
gulp.task('build:debug', 'Runs a debug build', ['clean', 'lint:js', 'concat:js']);
21-
gulp.task('build:release', 'Runs a release build', ['clean', 'lint:js', 'concat:js', 'min:js']);
15+
gulp.task('lint', 'Lints all files', ['lint:js', 'lint:spec']);
16+
gulp.task('test', 'Runs all tests', ['test:js']);
17+
gulp.task('build:debug', 'Runs a debug build', ['clean', 'lint', 'concat:js']);
18+
gulp.task('build:release', 'Runs a release build', ['clean', 'lint', 'concat:js', 'min:js']);
2219

23-
gulp.task('clean', function (callback) {
24-
rimraf(paths.jsDest, callback);
20+
gulp.task('clean', 'Cleans destination folder', function (done) {
21+
rimraf(paths.jsDest, done);
2522
});
2623

2724
gulp.task('lint:js', 'Lints all JavaScript', function () {
@@ -30,15 +27,30 @@ gulp.task('lint:js', 'Lints all JavaScript', function () {
3027
.pipe(jshint.reporter('jshint-stylish'));
3128
});
3229

33-
gulp.task('concat:js', 'Combines JavaScript files into a single file', function () {
30+
gulp.task('lint:spec', 'Lints all specs', function () {
31+
return gulp.src(paths.specSource)
32+
.pipe(jshint({
33+
predef: ['$', 'expect', 'beforeAll', 'afterAll']
34+
}))
35+
.pipe(jshint.reporter('jshint-stylish'));
36+
});
37+
38+
gulp.task('concat:js', 'Creates combined JavaScript file', function () {
3439
return gulp.src(paths.jsSource)
3540
.pipe(concat(paths.outFilename))
3641
.pipe(gulp.dest(paths.jsDest));
3742
});
3843

39-
gulp.task('min:js', 'Createds minified JavaScript file', ['concat:js'], function () {
44+
gulp.task('min:js', 'Creates minified JavaScript file', ['concat:js'], function () {
4045
return gulp.src(paths.jsDest + paths.outFilename)
4146
.pipe(uglify())
4247
.pipe(rename(paths.outMinFilename))
4348
.pipe(gulp.dest(paths.jsDest));
49+
});
50+
51+
gulp.task('test:js', 'Runs unit tests', function (done) {
52+
new karma.Server({
53+
configFile: __dirname + '/karma.conf.js',
54+
singleRun: process.env.DEBUG ? false : true
55+
}, done).start();
4456
});

‎karma.conf.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var paths = require('./paths');
2+
3+
module.exports = function (config) {
4+
config.set({
5+
frameworks: ['jasmine'],
6+
files: paths.karmaFiles,
7+
exclude: [],
8+
reporters: process.env.DEBUG ? ['spec'] : ['spec', 'coverage'],
9+
autoWatch: true,
10+
browsers: [process.env.DEBUG ? 'Chrome' : 'PhantomJS'],
11+
plugins: [
12+
'karma-chrome-launcher',
13+
'karma-jasmine',
14+
'karma-spec-reporter',
15+
'karma-phantomjs-launcher',
16+
'karma-coverage'
17+
],
18+
preprocessors: { './src/**/*.js': ['coverage'] },
19+
coverageReporter: {
20+
reporters: [
21+
{ type: 'html' },
22+
{ type: 'text-summary' }
23+
]
24+
},
25+
logLevel: config.LOG_INFO
26+
});
27+
};

‎package.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
"gulp-jshint": "^2.0.0",
2323
"gulp-rename": "^1.2.2",
2424
"gulp-uglify": "^1.5.1",
25+
"jasmine": "^2.4.1",
26+
"jasmine-core": "^2.4.1",
2527
"jshint": "^2.9.1",
2628
"jshint-stylish": "^2.1.0",
2729
"karma": "^0.13.19",
30+
"karma-chrome-launcher": "^0.2.2",
31+
"karma-coverage": "^0.5.3",
32+
"karma-jasmine": "^0.3.7",
33+
"karma-phantomjs-launcher": "^1.0.0",
34+
"karma-spec-reporter": "0.0.23",
35+
"phantomjs-prebuilt": "^2.1.3",
2836
"rimraf": "^2.5.1"
2937
}
3038
}

‎paths.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var paths = {
2+
outFilename: 'powerbi.js',
3+
outMinFilename: 'powerbi.min.js',
4+
jsSource: ['./src/embed.js', './src/tile.js', './src/report.js', './src/util.js', './src/core.js'],
5+
specSource: './test/**/*.spec.js',
6+
jsDest: './dist/'
7+
};
8+
9+
paths.vendorSource = ['./bower_components/jquery/dist/jquery.js'];
10+
paths.karmaFiles = paths.jsSource
11+
.concat(paths.vendorSource)
12+
.concat(paths.specSource);
13+
14+
module.exports = paths;

‎src/core.js‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
(function (powerbi) {
22
'use strict';
3-
3+
44
var embeds = [];
5+
var componentTypes = [];
56

67
powerbi.get = get;
7-
powerbi.embed = embed;
8+
powerbi.embed = embed;
9+
powerbi.init = init;
810

911
activate();
1012

11-
//////////////////////////////////
13+
//////////////////////////////////
1214

1315
function activate() {
14-
window.addEventListener('DOMContentLoaded', onLoad, false);
16+
window.addEventListener('DOMContentLoaded', init, false);
1517
window.addEventListener('message', onReceiveMessage, false);
16-
}
1718

18-
var componentTypes = [
19-
{ type: 'powerbi-tile', component: powerbi.Tile },
20-
{ type: 'powerbi-report', component: powerbi.Report }
21-
];
19+
componentTypes = [
20+
{ type: 'powerbi-tile', component: powerbi.Tile },
21+
{ type: 'powerbi-report', component: powerbi.Report }
22+
];
23+
}
2224

2325
var EmbedEventMap = {
2426
'tileClicked': 'tile-click',
2527
'tileLoaded': 'tile-load',
2628
'reportPageLoaded': 'report-load'
2729
};
28-
29-
function onLoad() {
30-
var components = document.querySelectorAll('[powerbi-embed]');
30+
31+
function init(container) {
32+
container = container || document.body;
33+
34+
var components = container.querySelectorAll('[powerbi-embed]');
3135
for (var i = 0; i < components.length; i++) {
3236
embed(components[i]);
3337
}
@@ -40,6 +44,10 @@
4044
function embed(element) {
4145
var instance;
4246

47+
if (element.powerBIEmbed) {
48+
return element.powerBIEmbed;
49+
}
50+
4351
for (var j = 0; j < componentTypes.length; j++) {
4452
var componentType = componentTypes[j];
4553

‎src/embed.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(function (powerbi) {
22
'use strict';
3-
3+
4+
powerbi.Embed = Embed;
5+
46
function Embed() { }
57

68
Embed.prototype = {

‎src/tile.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
powerbi.Tile = Tile;
5-
powerbi.Tile.prototype = powerbi.Embed.prototyp;
5+
powerbi.Tile.prototype = powerbi.Embed.prototype;
66

77
function Tile(element, options) {
88
var me = this;

‎test/core.spec.js‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
(function () {
2+
'use strict';
3+
4+
describe('powerbi', function () {
5+
beforeAll(function () {
6+
window.powerbi.accessToken = 'ABC123';
7+
$('<div id="powerbi-fixture"></div>').appendTo(document.body);
8+
});
9+
10+
afterAll(function () {
11+
$('#powerbi-fixture').remove();
12+
});
13+
14+
afterEach(function () {
15+
$('#powerbi-fixture').empty();
16+
});
17+
18+
it('is defined', function () {
19+
expect(window.powerbi).toBeDefined();
20+
});
21+
22+
describe('init', function () {
23+
it('embeds all components found in the DOM', function () {
24+
var elements = [
25+
'<div powerbi-embed="https://app.powerbi.com/reportEmbed?reportId=ABC123" powerbi-report></div>',
26+
'<div powerbi-embed="https://app.powerbi.com/embed?dashboardId=D1&tileId=T1" powerbi-tile></div>'
27+
];
28+
29+
for (var i = 0; i < elements.length; i++) {
30+
$(elements[i]).appendTo('#powerbi-fixture');
31+
}
32+
33+
window.powerbi.init();
34+
35+
var iframes = document.querySelectorAll('[powerbi-embed] iframe');
36+
expect(iframes.length).toEqual(2);
37+
});
38+
});
39+
40+
describe('get', function () {
41+
it('gets an instance of an already embedded component', function () {
42+
var component = $('<div powerbi-embed="https://app.powerbi.com/reportEmbed?reportId=ABC123" powerbi-report></div>')
43+
.appendTo('#powerbi-fixture');
44+
45+
window.powerbi.embed(component[0]);
46+
47+
var instance = window.powerbi.get(component[0]);
48+
expect(instance).toBeDefined();
49+
expect(instance instanceof window.powerbi.Report).toBe(true);
50+
});
51+
52+
it('creates and instance and returns it when it was note previously created', function () {
53+
var component = $('<div powerbi-embed="https://app.powerbi.com/reportEmbed?reportId=ABC123" powerbi-report></div>')
54+
.appendTo('#powerbi-fixture');
55+
56+
var instance = window.powerbi.get(component[0]);
57+
expect(instance).toBeDefined();
58+
expect(instance instanceof window.powerbi.Report).toBe(true);
59+
});
60+
});
61+
62+
describe('embed', function () {
63+
describe('reports', function () {
64+
it('creates report iframe from embedUrl', function () {
65+
var embedUrl = 'https://app.powerbi.com/reportEmbed?reportId=ABC123';
66+
var report = $('<div powerbi-embed="' + embedUrl + '" powerbi-report></div>')
67+
.appendTo('#powerbi-fixture');
68+
69+
window.powerbi.embed(report[0]);
70+
71+
var iframe = report.find('iframe');
72+
expect(iframe.length).toEqual(1);
73+
expect(iframe.attr('src')).toEqual(embedUrl);
74+
});
75+
76+
it('creates report iframe from report id', function () {
77+
var reportId = 'ABC123';
78+
var expectedEmbedUrl = 'https://app.powerbi.com/reportEmbed?reportId=' + reportId;
79+
var report = $('<div powerbi-embed powerbi-report="' + reportId + '"></div>')
80+
.appendTo('#powerbi-fixture');
81+
82+
window.powerbi.embed(report[0]);
83+
84+
var iframe = report.find('iframe');
85+
expect(iframe.length).toEqual(1);
86+
expect(iframe.attr('src')).toEqual(expectedEmbedUrl);
87+
});
88+
});
89+
90+
describe('tiles', function () {
91+
it('creates tile iframe from embedUrl', function () {
92+
var embedUrl = 'https://app.powerbi.com/embed?dashboardId=D1&tileId=T1';
93+
var tile = $('<div powerbi-embed="' + embedUrl + '" powerbi-tile></div>')
94+
.appendTo('#powerbi-fixture');
95+
96+
window.powerbi.embed(tile[0]);
97+
98+
var iframe = tile.find('iframe');
99+
expect(iframe.length).toEqual(1);
100+
expect(iframe.attr('src')).toEqual(embedUrl);
101+
});
102+
103+
it('creates tile iframe from dashboard & tile id', function () {
104+
var tileId = 'T1';
105+
var dashboardId = 'D1';
106+
var expectedTileUrl = 'https://app.powerbi.com/embed?dashboardId=' + dashboardId + '&tileId=' + tileId;
107+
var tile = $('<div powerbi-embed powerbi-tile="' + tileId + '" powerbi-dashboard="' + dashboardId + '"></div>')
108+
.appendTo('#powerbi-fixture');
109+
110+
window.powerbi.embed(tile[0]);
111+
112+
var iframe = tile.find('iframe');
113+
expect(iframe.length).toEqual(1);
114+
expect(iframe.attr('src')).toEqual(expectedTileUrl);
115+
});
116+
});
117+
});
118+
119+
describe('get', function () {
120+
121+
});
122+
});
123+
} ());

0 commit comments

Comments
 (0)