modifiy citation test to check for built version

This commit is contained in:
Josh de Leeuw 2024-12-17 15:58:30 -05:00
parent 5e878d381f
commit e42ba99ecc

View File

@ -1,4 +1,4 @@
import { JsPsych } from "../../src/JsPsych"; //import { JsPsych } from "../../src/JsPsych";
import { TestExtension } from "../extensions/TestExtension"; import { TestExtension } from "../extensions/TestExtension";
import TestPlugin from "../TestPlugin"; import TestPlugin from "../TestPlugin";
@ -6,30 +6,44 @@ const testPluginApaCitation = "Test plugin APA citation";
const testPluginBibtexCitation = "Test plugin BibTeX citation"; const testPluginBibtexCitation = "Test plugin BibTeX citation";
const testExtensionApaCitation = "Test extension APA citation"; const testExtensionApaCitation = "Test extension APA citation";
let jspsych: JsPsych;
let consoleLogSpy: jest.SpyInstance; let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance; let consoleWarnSpy: jest.SpyInstance;
beforeEach(() => { let JsPsych;
/**
* These tests are skipped if the built version of JsPsych is not found.
* This is because the citation functionality is only available in the built version
* due to code injections that run during the build.
*/
try {
// Try to import built version
JsPsych = require("../../dist/index").JsPsych;
let jspsych: typeof JsPsych;
beforeEach(() => {
jspsych = new JsPsych(); jspsych = new JsPsych();
consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(); consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
}); });
afterEach(() => { afterEach(() => {
jest.restoreAllMocks(); jest.restoreAllMocks();
}); });
describe("citing not using an array", () => { describe("citing not using an array", () => {
test("citing nothing", () => { test("citing nothing", () => {
expect(() => jspsych.getCitations(null)).toThrow("Expected array of plugins/extensions"); expect(() => jspsych.getCitations(null)).toThrow("Expected array of plugins/extensions");
}); });
test("citing without input and with invalid format", () => { test("citing without input and with invalid format", () => {
expect(() => jspsych.getCitations(null, "apa")).toThrow("Expected array of plugins/extensions"); expect(() => jspsych.getCitations(null, "apa")).toThrow(
"Expected array of plugins/extensions"
);
});
}); });
});
describe("citing using an array in different formats", () => { describe("citing using an array in different formats", () => {
test("citing empty array with APA format", () => { test("citing empty array with APA format", () => {
jspsych.getCitations([], "apa"); jspsych.getCitations([], "apa");
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided"); expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided");
@ -51,9 +65,9 @@ describe("citing using an array in different formats", () => {
"Unsupported citation format" "Unsupported citation format"
); );
}); });
}); });
describe("citing mix of valid plugins/extensions", () => { describe("citing mix of valid plugins/extensions", () => {
test("citing a plugin", () => { test("citing a plugin", () => {
jspsych.getCitations([TestPlugin]); jspsych.getCitations([TestPlugin]);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation); expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);
@ -74,4 +88,12 @@ describe("citing mix of valid plugins/extensions", () => {
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation); expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testExtensionApaCitation); expect(consoleLogSpy.mock.calls[1][0]).toBe(testExtensionApaCitation);
}); });
}); });
} catch (e) {
// Fall back to development version if built version not found
describe("skipping citation tests because of missing built version", () => {
test.skip("skip", () => {
expect(true).toBe(true);
});
});
}