Skip to content

Commit d207c70

Browse files
parthdagia05Parth
andauthored
Add unit tests for GetModename in IntervalsActions (#5463)
* feat: add new block type and update palette * test: add comprehensive GetModename tests in IntervalsActions.test.js * chore: revert unrelated changes to SaveInterface.test.js and IntervalsActions.js --------- Co-authored-by: Parth <dagia@Parths-MacBook-Pro.local>
1 parent b578a63 commit d207c70

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

js/turtleactions/__tests__/IntervalsActions.test.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,53 @@ describe("setupIntervalsActions", () => {
9898
setupIntervalsActions(activity);
9999
});
100100

101-
test("GetModename resolves modes", () => {
102-
expect(Singer.IntervalsActions.GetModename("major")).toBe("major");
103-
expect(Singer.IntervalsActions.GetModename("minor")).toBe("minor");
101+
describe("GetModename", () => {
102+
test("returns exact mode key from MUSICALMODES", () => {
103+
expect(Singer.IntervalsActions.GetModename("major")).toBe("major");
104+
expect(Singer.IntervalsActions.GetModename("minor")).toBe("minor");
105+
});
106+
107+
test("returns 'major' as fallback when no match is found", () => {
108+
expect(Singer.IntervalsActions.GetModename("nonexistent")).toBe("major");
109+
expect(Singer.IntervalsActions.GetModename("invalidMode")).toBe("major");
110+
});
111+
112+
test("handles invalid input gracefully", () => {
113+
expect(Singer.IntervalsActions.GetModename(null)).toBe("major");
114+
expect(Singer.IntervalsActions.GetModename(undefined)).toBe("major");
115+
expect(Singer.IntervalsActions.GetModename("")).toBe("major");
116+
expect(Singer.IntervalsActions.GetModename(123)).toBe("major");
117+
});
118+
119+
test("matches localized mode name via _() function", () => {
120+
// Mock _() to return a localized version
121+
global._ = x => (x === "minor" ? "moll" : x);
122+
123+
// When searching for localized name, it should find the original key
124+
expect(Singer.IntervalsActions.GetModename("moll")).toBe("minor");
125+
126+
// Reset the mock
127+
global._ = x => x;
128+
});
129+
130+
test("prefers exact key match over localized match", () => {
131+
// Create a fresh MUSICALMODES where "moll" comes before a mode that localizes to "moll"
132+
const originalModes = global.MUSICALMODES;
133+
global.MUSICALMODES = {
134+
moll: [2, 1, 2, 2, 1, 3, 1],
135+
minor: [2, 1, 2, 2, 1, 2, 2]
136+
};
137+
138+
// Mock _() to translate "minor" to "moll"
139+
global._ = x => (x === "minor" ? "moll" : x);
140+
141+
// "moll" should match the exact key "moll" since it's checked in the condition first
142+
expect(Singer.IntervalsActions.GetModename("moll")).toBe("moll");
143+
144+
// Restore original
145+
global.MUSICALMODES = originalModes;
146+
global._ = x => x;
147+
});
104148
});
105149

106150
test("GetIntervalNumber base case", () => {

0 commit comments

Comments
 (0)