-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathtest.ts
164 lines (150 loc) · 4.65 KB
/
test.ts
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
// tslint:disable-next-line: no-implicit-dependencies
import { get as scale } from "@tonaljs/scale";
// tslint:disable-next-line: no-implicit-dependencies
import { get as chord } from "@tonaljs/chord";
import * as Key from "./index";
describe("@tonal/key", () => {
test("fromAlter", () => {
expect(Key.majorTonicFromKeySignature("###")).toEqual("A");
expect(Key.majorTonicFromKeySignature(3)).toEqual("A");
expect(Key.majorTonicFromKeySignature("b")).toEqual("F");
expect(Key.majorTonicFromKeySignature("bb")).toEqual("Bb");
expect(Key.majorTonicFromKeySignature("other")).toEqual(null);
});
test("major keySignature", () => {
const tonics = "C D E F G A B".split(" ");
expect(
tonics.map((tonic) => Key.majorKey(tonic).keySignature).join(" "),
).toEqual(" ## #### b # ### #####");
});
test("minor keySignature", () => {
const tonics = "C D E F G A B".split(" ");
expect(
tonics.map((tonic) => Key.minorKey(tonic).keySignature).join(" "),
).toEqual("bbb b # bbbb bb ##");
});
describe("scale names", () => {
test("natural scales", () => {
const chordScales = Key.minorKey("C").natural.chordScales;
expect(chordScales.map(scale).map((scale) => scale.name)).toEqual([
"C minor",
"D locrian",
"Eb major",
"F dorian",
"G phrygian",
"Ab lydian",
"Bb mixolydian",
]);
});
test("harmonic scales", () => {
const chordScales = Key.minorKey("C").harmonic.chordScales;
expect(chordScales.map(scale).map((scale) => scale.name)).toEqual([
"C harmonic minor",
"D locrian 6",
"Eb major augmented",
"F lydian diminished",
"G phrygian dominant",
"Ab lydian #9",
"B ultralocrian",
]);
});
test("melodic scales", () => {
const chordScales = Key.minorKey("C").melodic.chordScales;
expect(chordScales.map(scale).map((scale) => scale.name)).toEqual([
"C melodic minor",
"D dorian b2",
"Eb lydian augmented",
"F lydian dominant",
"G mixolydian b6",
"A locrian #2",
"B altered",
]);
});
});
test("secondary dominants", () => {
expect(Key.majorKey("C").secondaryDominants).toEqual([
"",
"A7",
"B7",
"C7",
"D7",
"E7",
"",
]);
});
test("octaves are discarded", () => {
expect(Key.majorKey("b4").scale.join(" ")).toEqual("B C# D# E F# G# A#");
expect(Key.majorKey("g4").chords.join(" ")).toEqual(
"Gmaj7 Am7 Bm7 Cmaj7 D7 Em7 F#m7b5",
);
expect(Key.minorKey("C4").melodic.scale.join(" ")).toEqual(
"C D Eb F G A B",
);
expect(Key.minorKey("C4").melodic.chords.join(" ")).toEqual(
"Cm6 Dm7 Eb+maj7 F7 G7 Am7b5 Bm7b5",
);
});
test("valid chord names", () => {
const major = Key.majorKey("C");
const minor = Key.minorKey("C");
[
major.chords,
major.secondaryDominants,
major.substituteDominantSupertonics,
major.substituteDominants,
major.substituteDominantsMinorRelative,
minor.natural.chords,
minor.harmonic.chords,
minor.melodic.chords,
].forEach((chords) => {
chords.forEach((name) => {
if (name !== "") {
if (chord(name).name === "") throw Error(`Invalid chord: ${name}`);
expect(chord(name).name).not.toBe("");
}
});
});
});
test("C major", () => {
expect(Key.majorKey("C")).toMatchSnapshot();
});
test("C major chords", () => {
expect(Key.majorKeyChords("C")).toMatchSnapshot();
const chords = Key.majorKeyChords("C");
expect(chords.find((chord) => chord.name === "Em7")).toEqual({
name: "Em7",
roles: ["T", "ii/II"],
});
});
test.only("C minor chords", () => {
expect(Key.minorKeyChords("C")).toMatchSnapshot();
});
test("majorKeys", () => {
expect(Key.majorKey("A")).toMatchSnapshot();
expect(Key.majorKey("Bb")).toMatchSnapshot();
expect(Key.majorKey("E")).toMatchSnapshot();
});
test("empty major key ", () => {
expect(Key.majorKey("")).toMatchObject({
type: "major",
tonic: "",
});
expect(Object.keys(Key.majorKey("C")).sort()).toEqual(
Object.keys(Key.majorKey("")).sort(),
);
});
test("minorKey", () => {
expect(Key.minorKey("C")).toMatchSnapshot();
expect(Key.minorKey("Bb")).toMatchSnapshot();
expect(Key.minorKey("B")).toMatchSnapshot();
});
test("empty minor key ", () => {
expect(Key.minorKey("nothing")).toMatchObject({
type: "minor",
tonic: "",
});
expect(Object.keys(Key.minorKey("C")).sort()).toEqual(
Object.keys(Key.minorKey("nothing")).sort(),
);
});
});