Skip to content

Commit 57c808b

Browse files
committedJul 26, 2016
update tests to browser based
1 parent 93652d9 commit 57c808b

File tree

5 files changed

+87
-49
lines changed

5 files changed

+87
-49
lines changed
 

‎index.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Mocha</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="node_modules/mocha/mocha.css" />
8+
</head>
9+
<body>
10+
<main id="main">
11+
</main>
12+
<script src="index.js"></script>
13+
<!-- Open this file and call `mocha.run()` in console to run tests -->
14+
<div id="mocha"></div>
15+
<script src="node_modules/mocha/mocha.js"></script>
16+
<script src="node_modules/expect/umd/expect.min.js"></script>
17+
<script src="node_modules/sinon/pkg/sinon.js"></script>
18+
<script>mocha.setup('bdd');</script>
19+
<script src="index.js"></script>
20+
<script src="test/index-test.js"></script>
21+
22+
</body>
23+
</html>

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"homepage": "https://github.com/learn-co-curriculum/java-script-arithmetic-lab#readme",
2828
"devDependencies": {
29-
"expect": "^1.20.1",
29+
"expect": "^1.20.2",
3030
"jsdom": "^9.2.1",
3131
"mocha": "^2.5.3",
3232
"mocha-jsdom": "^1.1.0",

‎test/index-test.js

+36-48
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,54 @@
1-
const expect = require('expect')
2-
const fs = require('fs')
3-
const jsdom = require('mocha-jsdom')
4-
const path = require('path')
1+
var a, b
52

3+
beforeEach(function() {
4+
a = Math.floor(Math.random() * 1000)
5+
b = Math.floor(Math.random() * 1000)
6+
})
67

7-
describe('index', () => {
8-
jsdom({
9-
src: fs.readFileSync(path.resolve(__dirname, '..', 'index.js'), 'utf-8')
10-
})
8+
it('add(a, b) adds two numbers and returns the result', function() {
9+
expect(add(a, b)).toEqual(a + b)
10+
})
1111

12-
var a, b
12+
it('subtract(a, b) subtracts b from a and returns the result', function() {
13+
expect(subtract(a, b)).toEqual(a - b)
14+
})
1315

14-
beforeEach(() => {
15-
a = Math.floor(Math.random() * 1000)
16-
b = Math.floor(Math.random() * 1000)
17-
})
16+
it('multiply(a, b) multiplies two numbers and returns the result', function() {
17+
expect(multiply(a, b)).toEqual(a * b)
18+
})
1819

19-
it('add(a, b) adds two numbers and returns the result', () => {
20-
expect(add(a, b)).toEqual(a + b)
21-
})
20+
it('divide(a, b) divides a by b and returns the result', function() {
21+
expect(divide(a, b)).toEqual(a / b)
22+
})
2223

23-
it('subtract(a, b) subtracts b from a and returns the result', () => {
24-
expect(subtract(a, b)).toEqual(a - b)
25-
})
24+
it('inc(n) increments n and returns the result', function() {
25+
expect(inc(a)).toEqual(a + 1)
26+
})
2627

27-
it('multiply(a, b) multiplies two numbers and returns the result', () => {
28-
expect(multiply(a, b)).toEqual(a * b)
29-
})
28+
it('dec(n) decrements n and returns the result', function() {
29+
expect(dec(a)).toEqual(a - 1)
30+
})
3031

31-
it('divide(a, b) divides a by b and returns the result', () => {
32-
expect(divide(a, b)).toEqual(a / b)
32+
describe('makeInt(n)', function() {
33+
it('parses n as an integer and returns the parsed integer', function() {
34+
expect(makeInt(a.toString())).toEqual(a)
3335
})
3436

35-
it('inc(n) increments n and returns the result', () => {
36-
expect(inc(a)).toEqual(a + 1)
37+
it('assumes base 10', function() {
38+
expect(makeInt('0x2328')).toEqual(0)
3739
})
3840

39-
it('dec(n) decrements n and returns the result', () => {
40-
expect(dec(a)).toEqual(a - 1)
41+
it('returns NaN as appropriate', function() {
42+
expect(isNaN(makeInt('sldkjflksjf'))).toEqual(true)
4143
})
44+
})
4245

43-
describe('makeInt(n)', () => {
44-
it('parses n as an integer and returns the parsed integer', () => {
45-
expect(makeInt(a.toString())).toEqual(a)
46-
})
47-
48-
it('assumes base 10', () => {
49-
expect(makeInt('0x2328')).toEqual(0)
50-
})
51-
52-
it('returns NaN as appropriate', () => {
53-
expect(isNaN(makeInt('sldkjflksjf'))).toEqual(true)
54-
})
46+
describe('preserveDecimal(n)', function() {
47+
it('preserves n\'s decimals (it parses n as a floating point number) and returns the parsed number', function() {
48+
expect(preserveDecimal('2.222')).toEqual(2.222)
5549
})
5650

57-
describe('preserveDecimal(n)', () => {
58-
it('preserves n\'s decimals (it parses n as a floating point number) and returns the parsed number', () => {
59-
expect(preserveDecimal('2.222')).toEqual(2.222)
60-
})
61-
62-
it('returns NaN as appropriate', () => {
63-
expect(isNaN(preserveDecimal('sldkjflksjf'))).toEqual(true)
64-
})
51+
it('returns NaN as appropriate', function() {
52+
expect(isNaN(preserveDecimal('sldkjflksjf'))).toEqual(true)
6553
})
6654
})

‎test/mocha.opts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--timeout 10000

‎test/root.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
global.expect = require('expect');
2+
3+
const babel = require('babel-core');
4+
const jsdom = require('jsdom');
5+
const path = require('path');
6+
7+
8+
before(function(done) {
9+
const babelResult = babel.transformFileSync(path.resolve(__dirname, '..', 'index.js'), {
10+
presets: ['es2015']
11+
});
12+
13+
//const src = path.resolve(__dirname, '..', 'index.js');
14+
15+
jsdom.env('<div></div>', [], {src: babelResult.code}, (err, window) => {
16+
if (err) {
17+
return done(err);
18+
}
19+
20+
Object.keys(window).forEach(key => {
21+
global[key] = window[key];
22+
});
23+
24+
return done();
25+
});
26+
});

0 commit comments

Comments
 (0)
Please sign in to comment.