All files / test/exporter/asset CocosCreator.test.ts

95.45% Statements 63/66
100% Branches 0/0
90.91% Functions 20/22
95.45% Lines 63/66

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164  1x 1x 1x   1x     1x 1x   1x                         1x                           1x     2x       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 { SchemaJson, Sprite, Node } from '@drecom/scene-graph-schema';
import { expect } from 'chai';
import { spy } from 'sinon';
import { describe, it } from 'mocha';
 
import CocosCreator from '../../../src/exporter/asset/CocosCreator';
import AssetExportMapEntity from '../../../src/interface/AssetExportMapEntity';
 
const mockUrl  = 'http://127.0.0.1';
const mockPath = '/dev/null';
 
const nodeFixture: Node = {
  id: '',
  name: '',
  constructorName: '',
  transform: {
    x: 0,
    y: 0,
    anchor: {
      x:0,
      y: 0,
    }
  }
};
const sceneGraphFixture: SchemaJson = {
  scene: [
    nodeFixture
  ],
  metadata: {
    width: 0,
    height: 0,
    positiveCoord: {
      xRight: true,
      yDown: true
    }
  }
};
 
const mockPlugin = {
  extendSceneGraph: () => {},
  getExportMapExtendPaths: () => {
    return [];
  }
};
 
const sceneGraphMapFixture = new Map<string, SchemaJson>([['testGraph', sceneGraphFixture]]);
 
describe('AssetExporter::CocosCreator',  () => {
  let instance = new CocosCreator();
 
  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('createExportMap', () => {
    const subject = instance.createExportMap.bind(instance);
 
    describe('when plugins are given', () => {
      it('should invoke exposed pluginPostProcess method', () => {
        const pluginPostProcessSpy = spy(instance, 'pluginPostProcess');
 
        subject(sceneGraphMapFixture, mockPath, mockPath, 'testNameSPace', new Map([['testPlugin', mockPlugin]]));
        expect(pluginPostProcessSpy.getCalls().length).to.greaterThan(0);
 
        pluginPostProcessSpy.restore();
      });
      it('should invoke exposed plugin\'s getExportMapExtendPaths method', () => {
        const extendSceneGraphSpy = spy(mockPlugin, 'getExportMapExtendPaths');
 
        subject(sceneGraphMapFixture, mockPath, mockPath, 'testNameSPace', new Map([['testPlugin', mockPlugin]]));
        expect(extendSceneGraphSpy.getCalls().length).to.greaterThan(0);
 
        extendSceneGraphSpy.restore();
      });
    });
 
    describe('when plugins are not given', () => {
      it('should not invoke exposed getExportMapExtendPaths method', () => {
        const extendSceneGraphSpy = spy(mockPlugin, 'getExportMapExtendPaths');
 
        subject(sceneGraphMapFixture, mockPath, mockPath, 'testNameSPace');
        expect(extendSceneGraphSpy.getCalls().length).to.equal(0);
 
        extendSceneGraphSpy.restore();
      });
    });
  });
 
  describe('replacePaths', () => {
    const subject = instance.replacePaths.bind(instance);
    const subjectSprite: Sprite = {};
 
    const exportMapEntityFixture: AssetExportMapEntity = {
      localSrcPath:  '',
      localDestPath: '',
      url: mockUrl
    };
 
    describe('when scene graph node contains sprite', () => {
      nodeFixture.sprite = subjectSprite;
 
      describe('when sprite has url property', () => {
        subjectSprite.url = mockPath;
 
        it ('should set url of export map entity to scene graph node\'s url', () => {
          expect(subjectSprite.url).to.equal(mockPath);
 
          subject(
            new Map<string, SchemaJson>([['testGraph', sceneGraphFixture]]),
            new Map<string, AssetExportMapEntity>([[subjectSprite.url as string, exportMapEntityFixture]])
          );
 
          expect(subjectSprite.url).to.equal(mockUrl);
        });
      });
      describe('when sprite has atlasUrl property', () => {
        it ('should set url of export map entity to scene graph node\'s atlasUrl', () => {
          subjectSprite.atlasUrl = mockPath;
 
          it ('should set url of export map entity to scene graph node ', () => {
            expect(subjectSprite.atlasUrl).to.equal(mockPath);
 
            subject(
              new Map<string, SchemaJson>([['testGraph', sceneGraphFixture]]),
              new Map<string, AssetExportMapEntity>([[subjectSprite.atlasUrl as string, exportMapEntityFixture]])
            );
 
            expect(subjectSprite.atlasUrl).to.equal(mockUrl);
          });
        });
      });
    });
 
    describe('when plugins are given', () => {
      it('should invoke plugin\'s replaceExtendedPaths', () => {
        const replaceExtendedPathsSpy = spy();
 
        subject(
          new Map<string, SchemaJson>(),
          new Map<string, AssetExportMapEntity>(),
          new Map([['testPlugin', { replaceExtendedPaths: replaceExtendedPathsSpy }]])
        );
 
        expect(replaceExtendedPathsSpy.getCalls().length).to.greaterThan(0);
      });
    });
 
    describe('when plugins are not given', () => {
      it('should not invoke plugin\'s replaceExtendedPaths', () => {
        const replaceExtendedPathsSpy = spy();
 
        subject(new Map<string, SchemaJson>(), new Map<string, AssetExportMapEntity>());
 
        expect(replaceExtendedPathsSpy.getCalls().length).to.equal(0);
      });
    });
  });
});