All files / test/exporter/scene Unity.test.ts

94.29% Statements 66/70
100% Branches 0/0
90% Functions 18/20
94.29% Lines 66/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1181x 1x 1x 1x 1x   1x   1x   1x 1x   1x   1x 1x   1x 1x   1x 1x 1x       1x 1x   1x 1x 1x   1x 1x     1x 1x   1x 1x     1x 1x 1x   1x 1x   1x     1x 1x   1x 1x       1x 1x 1x   1x 1x   1x         1x 1x   1x 1x   1x 1x   1x 1x       1x 1x   1x 1x   1x 1x 1x 1x 1x 1x     1x 1x 1x 1x                      
import * as fs from 'fs';
import * as path from 'path';
import { expect } from 'chai';
import { fake, spy } from 'sinon';
import { describe, it, before, after } from 'mocha';
 
import * as yaml from 'yaml';
 
import Unity from '../../../src/exporter/scene/Unity';
 
const mockYamlFile = path.resolve(__dirname, '../../fixture/mock_scene.unity');
const MockSceneJson = fs.readFileSync(mockYamlFile, 'utf-8');
 
const mockPath = __filename;
 
describe('SceneExporter::Unity',  () => {
  let instance = new Unity();
 
  describe('getIdentifier', () => {
    const subject = instance.getIdentifier.bind(instance);
 
    it('should return not empty string', () => {
      expect(typeof subject()).to.equal('string');
      expect(subject().length).to.greaterThan(0);
    });
  });
 
  describe('createSceneGraphSchemas', () => {
    const subject = instance.createSceneGraphSchemas.bind(instance);
 
    before(() => {
      instance.loadSceneFile    = fake.returns({});
      instance.createSceneGraph = fake.returns({});
    });
    after(() => {
      instance = new Unity();
    });
 
    it('should invoke exposed api (loadSceneFile, createSceneGraph)', () => {
      subject([mockPath], __dirname);
 
      expect((instance.loadSceneFile as any).getCalls().length).to.greaterThan(0);
      expect((instance.createSceneGraph as any).getCalls().length).to.greaterThan(0);
    });
 
    describe('when plugins are given', () => {
      it('should invoke exposed pluginPostProcess method', () => {
        const pluginPostProcessSpy = spy(instance, 'pluginPostProcess');
 
        subject([mockPath], __dirname, new Map([['testPlugin', { extendSceneGraph: () => {}}]]));
        expect(pluginPostProcessSpy.getCalls().length).to.greaterThan(0);
 
        pluginPostProcessSpy.restore();
      });
 
      it('should invoke exposed plugin\'s extendSceneGraph method', () => {
        const extendSceneGraphSpy = spy();
 
        subject([mockPath], __dirname, new Map([['testPlugin', { extendSceneGraph: extendSceneGraphSpy }]]));
        expect(extendSceneGraphSpy.getCalls().length).to.greaterThan(0);
      });
    });
 
    describe('when plugins are not given', () => {
      it('should not invoke exposed pluginPostProcess method', () => {
        const pluginPostProcessSpy = spy(instance, 'pluginPostProcess');
 
        subject([mockPath], __dirname);
        expect(pluginPostProcessSpy.getCalls().length).to.equal(0);
 
        pluginPostProcessSpy.restore();
      });
    });
  });
 
  describe('pluginPostProcess', () => {
    const subject = instance.pluginPostProcess.bind(instance);
 
    it('should invoke extendSceneGraph of own plugins', () => {
      const extendSceneGraphSpy = spy();
 
      const plugins = new Map();
      plugins.set('testPlugin', { extendSceneGraph: extendSceneGraphSpy });
 
      subject({}, [], new Map(), plugins);
      expect(extendSceneGraphSpy.getCalls().length).to.greaterThan(0);
    });
  });
 
  describe('createSceneGraph', () => {
    const subject = instance.createSceneGraph.bind(instance);
 
    it('should return object with runtime specific metadata', () => {
      const graph = subject({});
 
      expect(graph.metadata.positiveCoord.xRight).to.equal(true);
      expect(graph.metadata.positiveCoord.yDown).to.equal(false);
      expect(graph.metadata.baseCoordinate.x).to.equal('center');
      expect(graph.metadata.baseCoordinate.y).to.equal('center');
      expect(graph.metadata.baseCoordinate.z).to.equal('center');
      expect(graph.metadata.format).to.equal(instance.getIdentifier());
    });
 
    it('should assign yaml document anchor as node id', () => {
      const graph = subject(MockSceneJson);
      const documents = yaml.parseAllDocuments(MockSceneJson);
      graph.scene.forEach((node: any) => {
        const anchors: string[] = [];
        documents.forEach((document) => {
          anchors.push(Object.keys((document.anchors as any).map)[0]);
        });
 
        expect(anchors.indexOf(node.id)).to.not.equal(-1);
      });
    });
  });
});