Skip to content

Commit

Permalink
fixes to browser Mocha testing, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
m1tk4 committed Dec 31, 2022
1 parent 4250800 commit c901a7e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 70 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ To run tests, make sure you run

The tests can be run in Node using:

npx mocha
npm test
npm run coverage

To run the tests in a browser environment, open the `test/smpte-timecode-test.html` file
in a browser.

## Update History
- 1.3.2
- fixed to browser-based tests, documentation.
- 1.3.1
- Coverage tests changed to nyc
- Support for fractional framerates and framerates above 60fps
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smpte-timecode",
"version": "1.3.1",
"version": "1.3.2",
"description": "JavaScript implementation of SMPTE timecode type",
"main": "smpte-timecode.js",
"scripts": {
Expand Down
49 changes: 28 additions & 21 deletions test/smpte-timecode-test.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Mocha Tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>

<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>

<script>mocha.setup('bdd')</script>
<script src="../smpte-timecode.js"></script>
<script src="smpte-timecode-test.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run();
</script>
</body>
<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
mocha.globals(['jQuery']);
</script>
<script src="../node_modules/expect.js/index.js"></script>
<script src="../node_modules/lodash/lodash.js"></script>
<script class="mocha-init">
var isEqual = _.isEqual;
</script>
<script src="../smpte-timecode.js"></script>
<script src="smpte-timecode-test.js"></script>
<script class="mocha-exec">
mocha.run();
</script>
</body>
</html>
102 changes: 55 additions & 47 deletions test/smpte-timecode-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
var sinon = require('sinon');
const isEqual = require('lodash.isequal');

// If we are running under Node, we need to add expect and load our module
if (typeof module !== 'undefined' && module.exports) {
global.expect = require('expect.js');
global.isEqual = require('lodash.isequal');
global.Timecode = require('../smpte-timecode.js');
global.sinon = require('sinon');
var runInBrowser = false;
}
else {
var runInBrowser = true;
}

describe('Constructor tests', function(){
Expand Down Expand Up @@ -221,50 +225,54 @@ describe('Date() operations', function(){
});

describe('DST handling', function() {
var clock;

function clearDate(d) {
d.setYear(0);
d.setMonth(0);
d.setDate(1);
}

function checkDst(d) {
// we need to fake out 'new Date()', since this issue only happens day of.
clock = sinon.useFakeTimers(d);

var t = new Timecode(d, 29.97, true);
var o = t.toDate();
// console.log(d.toString(), '->', o.toString());
clearDate(d);
clearDate(o);
expect(o.toString()).to.be(d.toString());
}

afterEach(function() {
clock.restore();
});

it ('handles DST start 1am', function() {
checkDst(new Date(2018,2,11,1,0,0,200));
checkDst(new Date(2018,2,11,1,59,59,200));
});

it ('handles DST start 2am', function() {
checkDst(new Date(2018,2,11,2,0,0,200));
checkDst(new Date(2018,2,11,2,59,59,200));
checkDst(new Date(2018,2,11,3,0,0,200));
});

it ('handles DST end 1am', function() {
checkDst(new Date(2018,10,4,1,0,0,200));
checkDst(new Date(2018,10,4,1,59,59,200));
});

it ('handles DST end 2am', function() {
checkDst(new Date(2018,10,4,2,0,0,200));
checkDst(new Date(2018,10,4,2,59,59,200));
checkDst(new Date(2018,10,4,3,0,0,200));
});
var clock;

function clearDate(d) {
d.setYear(0);
d.setMonth(0);
d.setDate(1);
}

function checkDst(d) {
// we need to fake out 'new Date()', since this issue only happens day of.
clock = sinon.useFakeTimers(d);

var t = new Timecode(d, 29.97, true);
var o = t.toDate();
// console.log(d.toString(), '->', o.toString());
clearDate(d);
clearDate(o);
expect(o.toString()).to.be(d.toString());
}

afterEach(function() {
if (!runInBrowser) clock.restore();
});

it ('handles DST start 1am', function() {
if (runInBrowser) this.skip();
checkDst(new Date(2018,2,11,1,0,0,200));
checkDst(new Date(2018,2,11,1,59,59,200));
});

it ('handles DST start 2am', function() {
if (runInBrowser) this.skip();
checkDst(new Date(2018,2,11,2,0,0,200));
checkDst(new Date(2018,2,11,2,59,59,200));
checkDst(new Date(2018,2,11,3,0,0,200));
});

it ('handles DST end 1am', function() {
if (runInBrowser) this.skip();
checkDst(new Date(2018,10,4,1,0,0,200));
checkDst(new Date(2018,10,4,1,59,59,200));
});

it ('handles DST end 2am', function() {
if (runInBrowser) this.skip();
checkDst(new Date(2018,10,4,2,0,0,200));
checkDst(new Date(2018,10,4,2,59,59,200));
checkDst(new Date(2018,10,4,3,0,0,200));
});

});

0 comments on commit c901a7e

Please sign in to comment.