|
| 1 | +const Turtles = require('../turtles'); |
| 2 | + |
| 3 | +global.createjs = { |
| 4 | + Container: jest.fn().mockImplementation(() => ({ |
| 5 | + addChild: jest.fn(), |
| 6 | + removeAllChildren: jest.fn(), |
| 7 | + on: jest.fn(), |
| 8 | + removeAllEventListeners: jest.fn(), |
| 9 | + })), |
| 10 | + Bitmap: jest.fn().mockImplementation(() => ({})), |
| 11 | +}; |
| 12 | + |
| 13 | +global.importMembers = jest.fn(); |
| 14 | +global.setupRhythmActions = jest.fn(); |
| 15 | +global.setupMeterActions = jest.fn(); |
| 16 | +global.setupPitchActions = jest.fn(); |
| 17 | +global.setupIntervalsActions = jest.fn(); |
| 18 | +global.setupToneActions = jest.fn(); |
| 19 | +global.setupOrnamentActions = jest.fn(); |
| 20 | +global.setupVolumeActions = jest.fn(); |
| 21 | +global.setupDrumActions = jest.fn(); |
| 22 | +global.setupDictActions = jest.fn(); |
| 23 | + |
| 24 | +global.Turtle = jest.fn().mockImplementation(() => ({ |
| 25 | + painter: { |
| 26 | + doSetHeading: jest.fn(), |
| 27 | + doSetPensize: jest.fn(), |
| 28 | + doSetChroma: jest.fn(), |
| 29 | + doSetValue: jest.fn(), |
| 30 | + doSetColor: jest.fn(), |
| 31 | + }, |
| 32 | + rename: jest.fn(), |
| 33 | + container: { |
| 34 | + scaleX: 1, |
| 35 | + scaleY: 1, |
| 36 | + scale: 1, |
| 37 | + on: jest.fn(), |
| 38 | + removeAllEventListeners: jest.fn(), |
| 39 | + }, |
| 40 | +})); |
| 41 | + |
| 42 | +describe('Turtles Class', () => { |
| 43 | + let activityMock; |
| 44 | + let turtles; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + activityMock = { |
| 48 | + stage: { addChild: jest.fn(), removeChild: jest.fn() }, |
| 49 | + refreshCanvas: jest.fn(), |
| 50 | + turtleContainer: new createjs.Container(), |
| 51 | + hideAuxMenu: jest.fn(), |
| 52 | + hideGrids: jest.fn(), |
| 53 | + _doCartesianPolar: jest.fn(), |
| 54 | + }; |
| 55 | + |
| 56 | + turtles = new Turtles(activityMock); |
| 57 | + turtles.activity = activityMock; |
| 58 | + turtles.getTurtleCount = jest.fn().mockReturnValue(0); |
| 59 | + turtles.getTurtle = jest.fn(() => ({ |
| 60 | + container: { |
| 61 | + scaleX: 1, |
| 62 | + scaleY: 1, |
| 63 | + scale: 1, |
| 64 | + }, |
| 65 | + })); |
| 66 | + |
| 67 | + turtles.pushTurtle = jest.fn(); |
| 68 | + turtles.addTurtleStageProps = jest.fn(); |
| 69 | + turtles.createArtwork = jest.fn(); |
| 70 | + turtles.createHitArea = jest.fn(); |
| 71 | + turtles.addTurtleGraphicProps = jest.fn(); |
| 72 | + turtles.isShrunk = jest.fn().mockReturnValue(false); |
| 73 | + document.body.innerHTML = '<div id="loader"></div>'; |
| 74 | + }); |
| 75 | + |
| 76 | + test('should initialize properly', () => { |
| 77 | + expect(turtles.activity).not.toBeUndefined(); |
| 78 | + expect(global.importMembers).toHaveBeenCalledWith(turtles, "", [activityMock]); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should call initActions on construction', () => { |
| 82 | + const spy = jest.spyOn(turtles, 'initActions'); |
| 83 | + turtles.initActions(); |
| 84 | + expect(spy).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should add a turtle properly', () => { |
| 88 | + turtles.addTurtle({}, { id: 1, name: 'TestTurtle' }); |
| 89 | + |
| 90 | + expect(turtles.getTurtleCount).toHaveBeenCalled(); |
| 91 | + expect(turtles.pushTurtle).toHaveBeenCalled(); |
| 92 | + expect(turtles.addTurtleStageProps).toHaveBeenCalled(); |
| 93 | + expect(turtles.createArtwork).toHaveBeenCalled(); |
| 94 | + expect(turtles.createHitArea).toHaveBeenCalled(); |
| 95 | + expect(turtles.addTurtleGraphicProps).toHaveBeenCalled(); |
| 96 | + expect(turtles.isShrunk).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + |
| 99 | + test('should toggle running state correctly', () => { |
| 100 | + turtles.markAllAsStopped(); |
| 101 | + expect(activityMock.refreshCanvas).toHaveBeenCalled(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments