Skip to content

Commit

Permalink
Adding test for the Turtleactions (#4227)
Browse files Browse the repository at this point in the history
  • Loading branch information
omsuneri authored Jan 5, 2025
1 parent 8ee8bb5 commit fd71cb8
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/turtleactions/DictActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,4 @@ function setupDictActions(activity) {
}
};
}
module.exports = setupDictActions;
1 change: 1 addition & 0 deletions js/turtleactions/OrnamentActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ function setupOrnamentActions(activity) {
}
};
}
module.exports = setupOrnamentActions;
88 changes: 88 additions & 0 deletions js/turtleactions/__tests__/DictActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const setupDictActions = require('../DictActions');
describe('setupDictActions', () => {
let activity;
let turtle;
let targetTurtle;

beforeAll(() => {
global.Turtle = {
DictActions: {}
};

global._ = jest.fn((key) => key);
});

beforeEach(() => {
activity = {
turtles: {
ithTurtle: jest.fn(),
screenX2turtleX: jest.fn(),
screenY2turtleY: jest.fn(),
},
logo: {
turtleDicts: {}
},
textMsg: jest.fn()
};

turtle = 0;
targetTurtle = {
painter: {
color: 'red',
value: 10,
chroma: 0.5,
pensize: 2,
font: 'Arial',
orientation: 90,
doSetColor: jest.fn(),
doSetValue: jest.fn(),
doSetChroma: jest.fn(),
doSetPensize: jest.fn(),
doSetFont: jest.fn(),
doSetHeading: jest.fn(),
doSetXY: jest.fn()
},
container: {
x: 100,
y: 200
},
singer: {
notesPlayed: [1, 2],
lastNotePlayed: ['C4'],
notePitches: ['C'],
noteOctaves: [4],
keySignature: 'C',
movable: true,
pitchNumberOffset: 0
}
};
activity.turtles.ithTurtle.mockReturnValue(targetTurtle);
activity.turtles.screenX2turtleX.mockReturnValue(100);
activity.turtles.screenY2turtleY.mockReturnValue(200);
setupDictActions(activity);
});

it('should set the turtle color correctly', () => {
Turtle.DictActions.SetDictValue(0, turtle, 'color', 'blue');
expect(targetTurtle.painter.doSetColor).toHaveBeenCalledWith('blue');
});

it('should get the turtle color correctly', () => {
const color = Turtle.DictActions._GetDict(0, turtle, 'color');
expect(color).toBe('red');
});

it('should return error message if dictionary does not exist', () => {
activity.logo.turtleDicts[turtle] = {};
const result = Turtle.DictActions.getValue('nonexistentDict', 'color', turtle);
expect(result).toBe('Dictionary with this name does not exist');
});

it('should set and get values in the turtle dictionary', () => {
activity.logo.turtleDicts[turtle] = {};
Turtle.DictActions.setValue('customDict', 'color', 'green', turtle);
expect(activity.logo.turtleDicts[turtle].customDict.color).toBe('green');
const value = Turtle.DictActions.getValue('customDict', 'color', turtle);
expect(value).toBe('green');
});
});
82 changes: 82 additions & 0 deletions js/turtleactions/__tests__/OrnamentActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const setupOrnamentActions = require('../OrnamentActions');
describe('OrnamentActions', () => {
let activity, turtleMock;

beforeEach(() => {
global.Singer = {
OrnamentActions: null,
};

turtleMock = {
singer: {
staccato: [],
justCounting: [],
inNeighbor: [],
neighborStepPitch: [],
neighborNoteValue: [],
},
};

activity = {
turtles: {
ithTurtle: jest.fn(() => turtleMock),
},
blocks: {
blockList: { 1: {} },
},
logo: {
setDispatchBlock: jest.fn(),
setTurtleListener: jest.fn(),
notation: {
notationBeginSlur: jest.fn(),
notationEndSlur: jest.fn(),
},
},
};

global.MusicBlocks = { isRun: true };
global.Mouse = {
getMouseFromTurtle: jest.fn(() => ({
MB: { listeners: [] },
})),
};
setupOrnamentActions(activity);
});

test('setStaccato sets up staccato properly', () => {
Singer.OrnamentActions.setStaccato(2, 0, 1);
expect(turtleMock.singer.staccato).toContain(1 / 2);
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_staccato_0');
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(
0,
'_staccato_0',
expect.any(Function)
);
});

test('setSlur sets up slur properly', () => {
Singer.OrnamentActions.setSlur(2, 0, 1);
expect(turtleMock.singer.staccato).toContain(-1 / 2);
expect(activity.logo.notation.notationBeginSlur).toHaveBeenCalledWith(0);
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_staccato_0');
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(
0,
'_staccato_0',
expect.any(Function)
);
});

test('doNeighbor sets up neighbor action properly', () => {
Singer.OrnamentActions.doNeighbor(3, 4, 0, 1);
expect(turtleMock.singer.inNeighbor).toContain(1);
expect(turtleMock.singer.neighborStepPitch).toContain(3);
expect(turtleMock.singer.neighborNoteValue).toContain(4);
expect(activity.logo.setDispatchBlock).toHaveBeenCalledWith(1, 0, '_neighbor_0_1');
expect(activity.logo.setTurtleListener).toHaveBeenCalledWith(
0,
'_neighbor_0_1',
expect.any(Function)
);
});
});

0 comments on commit fd71cb8

Please sign in to comment.