jsPsych/packages/jspsych/tests/utils/utils.test.ts
bjoluc c119650471 Move experiment execution into jsPsych.run()
The `run()` method takes a timeline and returns a promise that is
resolved when the experiment finishes.
Hence, jsPsych can now be initialized without starting an experiment.
This re-enables usage of `jsPsych.timelineVariable()` in timeline
definitions and repairs exclusion checks and extension loading.
2021-07-19 17:10:37 +02:00

64 lines
1.5 KiB
TypeScript

import { deepCopy, flatten, unique } from "../../src/modules/utils";
describe("unique", () => {
test("generates unique array when there are duplicates", () => {
var arr = [1, 1, 2, 2, 3, 3];
var out = unique(arr);
expect(out).toEqual([1, 2, 3]);
expect(out).not.toEqual(arr);
});
test("generates same array when there are no duplicates", () => {
var arr = [1, 2, 3];
var out = unique(arr);
expect(out).toEqual(arr);
});
});
describe("flatten", () => {
test("generates flat array from flat input", () => {
var arr = [1, 1, 2, 2, 3, 3];
var out = flatten(arr);
expect(out).toEqual(arr);
});
test("generates flat array from nested input", () => {
var arr = [1, [1, 2, 2], [3], 3];
var out = flatten(arr);
expect(out).toEqual([1, 1, 2, 2, 3, 3]);
});
});
describe("deepCopy", () => {
test("works for objects", () => {
var o = { a: 1, b: { c: 2, d: 3 } };
var o2 = deepCopy(o);
o2.b.c = 4;
expect(o.b.c).toBe(2);
});
test("works for objects with arrays", () => {
var o = { a: 1, b: [2, 3] };
var o2 = deepCopy(o);
o2.b[0] = 4;
expect(JSON.stringify(o2.b)).toBe(JSON.stringify([4, 3]));
expect(o.b[0]).toBe(2);
});
test("works for objects with functions", () => {
var c = 0;
var o = {
a: 1,
b: () => {
c = 2;
},
};
var o2 = deepCopy(o);
o2.b = () => {
c = 1;
};
o.b();
expect(c).toBe(2);
o2.b();
expect(c).toBe(1);
});
});