Skip to content

Commit

Permalink
Migrate more unit tests from Karma to Node (#4163)
Browse files Browse the repository at this point in the history
  • Loading branch information
willeastcott authored Apr 4, 2022
1 parent c696df8 commit 3156030
Show file tree
Hide file tree
Showing 14 changed files with 731 additions and 1,083 deletions.

Large diffs are not rendered by default.

55 changes: 29 additions & 26 deletions test/i18n/i18n.test.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Application } from '../../src/framework/app-base.js';
import { Asset } from '../../src/asset/asset.js';
import { JsonHandler } from '../../src/resources/json.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';

import { expect } from 'chai';
import sinon from 'sinon';

describe('I18n', function () {

Expand All @@ -16,6 +18,7 @@ describe('I18n', function () {

afterEach(function () {
app.destroy();
sinon.restore();
});

const DEFAULT_LOCALE_FALLBACKS = {
Expand Down Expand Up @@ -580,9 +583,9 @@ describe('I18n', function () {
});

it.skip('assets not in asset registry get loaded after they are added to the registry', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

const asset = new Asset('a1', 'json', { url: '/fake/url.json' });
app.i18n.assets = [asset];
Expand All @@ -597,9 +600,9 @@ describe('I18n', function () {
});

it.skip('assets in asset registry get loaded when passed to i18n', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

const asset = new Asset('a1', 'json', { url: '/fake/url.json' });
app.assets.add(asset);
Expand All @@ -614,10 +617,10 @@ describe('I18n', function () {
app.assets.load(asset);
});

it.skip('assets already loaded are parsed when passed to i18n', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
it('assets already loaded are parsed when passed to i18n', function (done) {
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

app.i18n.on('data:add', function () {
expect(app.i18n.getText('key')).to.equal('translation');
Expand All @@ -633,10 +636,10 @@ describe('I18n', function () {
app.assets.load(asset);
});

it.skip('translations are unloaded when the asset is unloaded', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
it('translations are unloaded when the asset is unloaded', function (done) {
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

const asset = new Asset('a1', 'json', { url: '/fake/url.json' });

Expand All @@ -654,10 +657,10 @@ describe('I18n', function () {
app.assets.load(asset);
});

it.skip('translations are unloaded when the asset is removed', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
it('translations are unloaded when the asset is removed', function (done) {
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

const asset = new Asset('a1', 'json', { url: '/fake/url.json' });

Expand All @@ -675,10 +678,10 @@ describe('I18n', function () {
app.assets.load(asset);
});

it.skip('translations are re-loaded when the asset is removed and then added again', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
it('translations are re-loaded when the asset is removed and then added again', function (done) {
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

const asset = new Asset('a1', 'json', { url: '/fake/url.json' });

Expand All @@ -700,10 +703,10 @@ describe('I18n', function () {
app.assets.load(asset);
});

it.skip('translations are re-loaded when the contents of the asset change', function (done) {
// sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
// callback(null, createTranslation('en-US', 'key', 'translation'));
// });
it('translations are re-loaded when the contents of the asset change', function (done) {
sinon.stub(JsonHandler.prototype, 'load').callsFake(function (url, callback) {
callback(null, createTranslation('en-US', 'key', 'translation'));
});

const asset = new Asset('a1', 'json', { url: '/fake/url.json' });

Expand Down
66 changes: 66 additions & 0 deletions test/net/http.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { http, Http } from '../../src/net/http.js';

import { expect } from 'chai';
import sinon from 'sinon';

describe('Http', function () {
let retryDelay;

beforeEach(function () {
retryDelay = Http.retryDelay;
Http.retryDelay = 1;
});

afterEach(function () {
Http.retryDelay = retryDelay;
sinon.restore();
});

describe('#get()', function () {

it('returns resource', function (done) {
http.get('http://localhost:3000/test/test-assets/test.json', function (err, data) {
expect(err).to.equal(null);
expect(data).to.deep.equal({
a: 1,
b: true,
c: 'hello world'
});
done();
});
});

it('does not retry if retry is false', function (done) {
sinon.spy(http, 'request');
http.get('http://localhost:3000/someurl.json', function (err, data) {
expect(err).to.equal(404);
expect(http.request.callCount).to.equal(1);
done();
});
});

it('retries resource and returns 404 in the end if not found', function (done) {
sinon.spy(http, 'request');
http.get('http://localhost:3000/someurl.json', {
retry: true,
maxRetries: 2
}, function (err) {
expect(err).to.equal(404);
expect(http.request.callCount).to.equal(3);
done();
});
});

it('retries resource 5 times by default', function (done) {
sinon.spy(http, 'request');
http.get('http://localhost:3000/someurl.json', {
retry: true
}, function (err) {
expect(http.request.callCount).to.equal(6);
done();
});
});

});

});
103 changes: 103 additions & 0 deletions test/scene/batching/batch-manager.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Application } from '../../../src/framework/app-base.js';
import { Entity } from '../../../src/framework/entity.js';
import { LAYERID_WORLD } from '../../../src/scene/constants.js';

import { HTMLCanvasElement } from '@playcanvas/canvas-mock';

import { expect } from 'chai';

describe('BatchManager', function () {

beforeEach(function () {
const canvas = new HTMLCanvasElement(500, 500);
this.app = new Application(canvas);

this.bg = this.app.batcher.addGroup('Test Group', false, 100);
});

afterEach(function () {
this.app.destroy();
});

it('generate: removes model component mesh instances from layer', function () {
const e1 = new Entity();
e1.name = 'e1';
e1.addComponent('model', {
type: 'box',
batchGroupId: this.bg.id
});

const e2 = new Entity();
e2.name = 'e2';
e2.addComponent('model', {
type: 'box',
batchGroupId: this.bg.id
});

this.app.root.addChild(e1);
this.app.root.addChild(e2);

this.app.batcher.generate();

const layer = this.app.scene.layers.getLayerById(LAYERID_WORLD);
const instances = layer.opaqueMeshInstances;

expect(instances.length).to.equal(1);
expect(instances[0]).not.to.equal(e1.model.meshInstances[0]);
expect(instances[1]).not.to.equal(e2.model.meshInstances[0]);
});

it('disable model component, marks batch group dirty', function () {
const e1 = new Entity();
e1.name = 'e1';
e1.addComponent('model', {
type: 'box',
batchGroupId: this.bg.id
});

const e2 = new Entity();
e2.name = 'e2';
e2.addComponent('model', {
type: 'box',
batchGroupId: this.bg.id
});

this.app.root.addChild(e1);
this.app.root.addChild(e2);

this.app.batcher.generate();

e2.enabled = false;

expect(this.app.batcher._dirtyGroups[0]).to.equal(this.bg.id);
});


it('batch with all invisible meshinstances works', function () {
const e1 = new Entity();
e1.name = 'e1';
e1.addComponent('model', {
type: 'box',
batchGroupId: this.bg.id
});

const e2 = new Entity();
e2.name = 'e2';
e2.addComponent('model', {
type: 'box',
batchGroupId: this.bg.id
});


e1.model.meshInstances[0].visible = false;
e2.model.meshInstances[0].visible = false;

this.app.root.addChild(e1);
this.app.root.addChild(e2);

this.app.batcher.generate();

expect(this.app.batcher._batchList.length).to.equal(0);

});
});
Loading

0 comments on commit 3156030

Please sign in to comment.