Skip to content

Commit

Permalink
jest
Browse files Browse the repository at this point in the history
  • Loading branch information
ataromoku committed Jun 20, 2024
1 parent 540f5e4 commit 931d808
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 61 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

19 changes: 19 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
presets: [
["@babel/preset-env", {
"useBuiltIns": "entry",
}],
[
"@babel/preset-react",
{
//"pragma": "dom", // default pragma is React.createElement (only in classic runtime)
//"pragmaFrag": "DomFrag", // default is React.Fragment (only in classic runtime)
"throwIfNamespace": false, // defaults to true
"runtime": "automatic" // defaults to classic
// "importSource": "custom-jsx-library" // defaults to react (only in automatic runtime)
}
]
],
// sourceMaps: "inline",
plugins: ['babel-plugin-rewire']
};
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
setupFilesAfterEnv: [

],
testEnvironment: "jsdom",
transform: {
'^.+\\.js$': 'babel-jest',
},
transformIgnorePatterns: [
"node_modules/(?!(\@?lit|lit-html|lit-element|sinon|react-valence-ui-iframe|react-frau-intl)/)"
],
testPathIgnorePatterns: [
"/__tests__/utils/"
],
moduleFileExtensions: ['js', 'jsx', 'json', 'node']
};
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
"react-dom": "^0.14.0 || ^15.0.0"
},
"dependencies": {
"babel-core": "^6.26.3",
"bower": "^1.6.5",
"d2l-intl": "^0.2.1",
"react-frau-intl": "^0.2.0",
"react-valence-ui-iframe": "github:Brightspace/react-valence-ui-iframe#semver:^4",
"react-valence-ui-iframe": "github:Brightspace/react-valence-ui-iframe#tests-fix",
"url-parse": "^1.0.5"
},
"devDependencies": {
"babel-core": "^6.9.0",
"@babel/preset-env": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"babel-plugin-rewire": "^1.0.0-rc-3",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babelify": "^7.3.0",
"browserify": "^14.0.0",
"browserify-istanbul": "^3.0.0",
"browserify-shim": "^3.8.11",
"babelify": "^10.0.0",
"browserify": "^17.0.0",
"browserify-istanbul": "^3.0.1",
"browserify-shim": "^3.8.16",
"coveralls": "^3.1.1",
"es6-promise": "^3.2.1",
"eslint": "^2.3.0",
Expand All @@ -58,7 +58,9 @@
"esmify": "^2.1.1",
"http-server": "^0.8.5",
"istanbul": "gotwarlost/istanbul#source-map",
"jasmine-core": "^2.4.1",
"jasmine-core": "^5.1.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"karma": "^6.4.0",
"karma-browserify": "^8.1.0",
"karma-chrome-launcher": "^3.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/generic/__tests__/genericPluginTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Generic Plugin', function() {
});

it('should return a view', function() {
var viewer = GenericPlugin.getComponent();
var viewer = GenericPlugin.getComponent({src: 'test'});
expect(viewer).toBeDefined();
});

Expand Down
46 changes: 24 additions & 22 deletions src/plugins/generic/__tests__/viewerTests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var React = require('react'), //eslint-disable-line no-unused-vars
TestUtils = require( 'react-addons-test-utils' ),
var React = require('react'), //eslint-disable-line no-unused-
ReactTestUtils = require('react-dom/test-utils'),
Viewer = require('../viewer.js'),
Icon = require('../icon.js'),
Size = require('../size.js'),
Expand All @@ -16,9 +16,9 @@ describe('Generic Viewer', function() {

beforeEach(function() {
sandbox = sinon.sandbox.create();
sandbox.stub(Icon.prototype, 'render').returns(<div></div>);
sandbox.stub(Download.prototype, 'render').returns(<div></div>);
sandbox.stub(Size.prototype, 'render').returns(<div></div>);
// sandbox.stub(Icon.prototype, 'render').returns(<div></div>);
// sandbox.stub(Download.prototype, 'render').returns(<div></div>);
// sandbox.stub(Size.prototype, 'render').returns(<div></div>);

ViewerTester = stubIntlMessage(
Viewer,
Expand All @@ -40,21 +40,23 @@ describe('Generic Viewer', function() {
});

it('should render wrapper with expected class name', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(

<ViewerTester mimeType='audio/mp3' srcdownload='foo.mp3' />
);
var wrapper = TestUtils.findRenderedDOMComponentWithClass(
var wrapper = ReactTestUtils.findRenderedDOMComponentWithClass(
elem.refs.component,
'vui-fileviewer-generic'
);
expect(wrapper).toBeDefined();
});

it('should render a download area if fileInfoProvider returns data', function() {
var elem = TestUtils.renderIntoDocument(
//ReactTestUtils.renderIntoDocument(<input></input>);
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='audio/mp3' srcdownload='foo.mp3' />
);
var wrapper = TestUtils.scryRenderedDOMComponentsWithClass(
var wrapper = ReactTestUtils.scryRenderedDOMComponentsWithClass(
elem.refs.component,
'generic-download-area'
);
Expand All @@ -64,40 +66,40 @@ describe('Generic Viewer', function() {
it('should call the resizeCallback function', function() {
var resizeCallback = sinon.stub();

TestUtils.renderIntoDocument(
ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='audio/mp3' srcdownload='foo.mp3' resizeCallback={resizeCallback}/>
);

expect(resizeCallback.called).toBe(true);
});

it('should not render a download area if fileInfoProvider returns an error', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='audio/mp3' srcdownload='foo.bar' />
);
var wrapper = TestUtils.scryRenderedDOMComponentsWithClass(
var wrapper = ReactTestUtils.scryRenderedDOMComponentsWithClass(
elem.refs.component,
'generic-download-area'
);
expect(wrapper[0]).not.toBeDefined();
});

it('should not render if fileInfoProvider returns no file info', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='audio/mp3' srcdownload='file1.gif' />
);
var wrapper = TestUtils.scryRenderedDOMComponentsWithClass(
var wrapper = ReactTestUtils.scryRenderedDOMComponentsWithClass(
elem.refs.component,
'vui-fileviewer-generic'
);
expect(wrapper[0]).not.toBeDefined();
});

it('should use the mimetype from the download if it exists', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester srcdownload='foo.mp3' />
);
var wrapper = TestUtils.findRenderedComponentWithType(
var wrapper = ReactTestUtils.findRenderedComponentWithType(
elem.refs.component,
Icon
);
Expand All @@ -106,10 +108,10 @@ describe('Generic Viewer', function() {
});

it('should use the mimetype passed in as a prop if the download one does not exist', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='testType' srcdownload='foo2.mp3' />
);
var wrapper = TestUtils.findRenderedComponentWithType(
var wrapper = ReactTestUtils.findRenderedComponentWithType(
elem.refs.component,
Icon
);
Expand All @@ -118,10 +120,10 @@ describe('Generic Viewer', function() {
});

it('should render the download button to the screen', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='testType' srcdownload='foo2.mp3' />
);
var wrapper = TestUtils.findRenderedComponentWithType(
var wrapper = ReactTestUtils.findRenderedComponentWithType(
elem.refs.component,
Download
);
Expand All @@ -130,10 +132,10 @@ describe('Generic Viewer', function() {
});

it('should render the file Size to the screen', function() {
var elem = TestUtils.renderIntoDocument(
var elem = ReactTestUtils.renderIntoDocument(
<ViewerTester mimeType='testType' srcdownload='foo.mp3' />
);
var wrapper = TestUtils.findRenderedComponentWithType(
var wrapper = ReactTestUtils.findRenderedComponentWithType(
elem.refs.component,
Size
);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/html/__tests__/htmlPluginTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Html Plugin', function() {
});

it('should return a view', function() {
var viewer = HtmlPlugin.getComponent();
var viewer = HtmlPlugin.getComponent({src: 'test.html'});
expect(viewer).toBeDefined();
});

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/image/__tests__/imagePluginTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Image Plugin', function() {
});

it('should return a view', function() {
var viewer = ImagePlugin.getComponent();
var viewer = ImagePlugin.getComponent({src: 'test'});
expect(viewer).toBeDefined();
});

Expand Down
1 change: 1 addition & 0 deletions src/plugins/pdf/alternate/__mocks__/pdfjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
29 changes: 13 additions & 16 deletions src/plugins/pdf/alternate/__tests__/pdfjsWorkerSrcInitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ describe('pdfjsWorkerSrcInit', function() {
});

describe('', function() {
beforeEach(function(done) {
var promise = pdfjsWorkerSrcInit();
beforeEach(async () => {
await pdfjsWorkerSrcInit();
requests[0].respond(200, { 'Content-Type': 'application/javascript' }, 'var x = 10000;');
promise.then(done);
});
}, 50000);

it('should get worker js file and set workerSrc to an object URL', function() {
expect(requests.length).toBe(1);
Expand All @@ -53,12 +52,11 @@ describe('pdfjsWorkerSrcInit', function() {
});

describe('', function() {
beforeEach(function(done) {
var promise1 = pdfjsWorkerSrcInit();
var promise2 = pdfjsWorkerSrcInit();
beforeEach(async () => {
await pdfjsWorkerSrcInit();
await pdfjsWorkerSrcInit();
requests[0].respond(200, { 'Content-Type': 'application/javascript' }, 'var x = 10000;');
return Promise.all([promise1, promise2]).then(done);
});
}, 50000);

it('should not get worker js file if it has already been retrieved', function() {
expect(requests.length).toBe(1);
Expand All @@ -68,11 +66,10 @@ describe('pdfjsWorkerSrcInit', function() {
});

describe('', function() {
beforeEach(function(done) {
var promise = pdfjsWorkerSrcInit();
beforeEach(async () => {
await pdfjsWorkerSrcInit();
requests[0].respond(404);
return promise.then(done);
});
}, 50000);

it('should not get worker js file if it has already been retrieved', function() {
expect(requests.length).toBe(1);
Expand All @@ -82,10 +79,10 @@ describe('pdfjsWorkerSrcInit', function() {
});

describe('', function() {
beforeEach(function(done) {
beforeEach(async () => {
workerSrcIsCrossDomain = false;
return pdfjsWorkerSrcInit().then(done);
});
await pdfjsWorkerSrcInit();
}, 50000);

it('should not get worker js file if it has already been retrieved', function() {
expect(requests.length).toBe(0);
Expand Down
16 changes: 8 additions & 8 deletions src/plugins/pdf/native/__tests__/nativePluginTests.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
'use strict';

var native = require('../native.js');
var plugin = require('../native.js');

describe('PDF Native Plugin', function() {

it('does not handle non-PDF files', function() {
native.__Rewire__('supportsNativePdf', function() { return true; });
var result = native.test('foo/bar');
plugin.__Rewire__('supportsNativePdf', function() { return true; });
var result = plugin.test('foo/bar');
expect(result).toBeFalsy();
});

it('does not handle PDF files if no native support', function() {
native.__Rewire__('supportsNativePdf', function() { return false; });
var result = native.test('application/pdf');
plugin.__Rewire__('supportsNativePdf', function() { return false; });
var result = plugin.test('application/pdf');
expect(result).toBeFalsy();
});

it('does handle PDF files if native support', function() {
native.__Rewire__('supportsNativePdf', function() { return true; });
var result = native.test('application/pdf');
plugin.__Rewire__('supportsNativePdf', function() { return true; });
var result = plugin.test('application/pdf');
expect(result).toBeTruthy();
});

it('returns a viewer', function() {
var viewer = native.getComponent();
var viewer = plugin.getComponent({src: 'test'});
expect(viewer).toBeDefined();
});

Expand Down

0 comments on commit 931d808

Please sign in to comment.