All files / src sprite.spec.js

100% Statements 85/85
100% Branches 0/0
100% Functions 3/3
100% Lines 85/85

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 861x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 5x 3x 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 1x 1x  
import { expect } from 'chai';
import { Sprite } from './sprite.js';
 
// fake canvas provider for unit testing
const canvasProvider = () => ({
  width: 100,
  height: 100,
  style: { },
  getContext: () => ({
    drawImage: () => {},
  }),
});
 
describe('loadSpriteData', () => {
  it('should set the internal canvas size according to different scales', () => {
    const image = { width: 20, height: 20 };
    const spriteData = Sprite.loadSpriteData({
      image, rows: 2, columns: 2, padding: 2, canvasProvider, scales: [10, 20],
    });
    expect(spriteData).to.have.all.keys(10, 20);
    expect(spriteData[10].canvas.width).to.equal(20);
    expect(spriteData[20].canvas.width).to.equal(40);
  });
  it('should load sprite data into parts', () => {
    const image = { width: 20, height: 20 };
    const spriteData = Sprite.loadSpriteData({
      image, rows: 2, columns: 2, padding: 2, canvasProvider, scales: [10],
    });
    expect(spriteData[10].canvas).not.null;
    expect(spriteData[10].parts.length).to.eql(4);
    expect(spriteData[10].parts).to.have.deep.members([
      {
        x: 2, y: 2, width: 6, height: 6,
      },
      {
        x: 12, y: 2, width: 6, height: 6,
      },
      {
        x: 2, y: 12, width: 6, height: 6,
      },
      {
        x: 12, y: 12, width: 6, height: 6,
      },
    ]);
  });
 
  it('should load sprite data with different scales', () => {
    const image = { width: 20, height: 20 };
    const spriteData = Sprite.loadSpriteData({
      image, rows: 2, columns: 2, padding: 2, canvasProvider, scales: [10, 20],
    });
    expect(spriteData[10].canvas).not.null;
    expect(spriteData[10].parts.length).to.eql(4);
    expect(spriteData[10].parts).to.have.deep.members([
      {
        x: 2, y: 2, width: 6, height: 6,
      },
      {
        x: 12, y: 2, width: 6, height: 6,
      },
      {
        x: 2, y: 12, width: 6, height: 6,
      },
      {
        x: 12, y: 12, width: 6, height: 6,
      },
    ]);
    expect(spriteData[20].canvas).not.null;
    expect(spriteData[20].parts.length).to.eql(4);
    expect(spriteData[20].parts).to.have.deep.members([
      {
        x: 4, y: 4, width: 12, height: 12,
      },
      {
        x: 24, y: 4, width: 12, height: 12,
      },
      {
        x: 4, y: 24, width: 12, height: 12,
      },
      {
        x: 24, y: 24, width: 12, height: 12,
      },
    ]);
  });
});