Skip to content

Commit

Permalink
Merge branch 'add-citation-module' of github.com:jspsych/jsPsych into…
Browse files Browse the repository at this point in the history
… add-citation-module
  • Loading branch information
cherriechang committed Dec 18, 2024
2 parents f2d1a03 + e42ba99 commit 466b44c
Showing 1 changed file with 83 additions and 67 deletions.
150 changes: 83 additions & 67 deletions packages/jspsych/tests/citations/citations.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initJsPsych } from "../../dist/index.js";
//import { JsPsych } from "../../src/JsPsych";
import { TestExtension } from "../extensions/TestExtension";
import TestPlugin from "../TestPlugin";

Expand All @@ -10,78 +10,94 @@ const testPluginApaCitation = "Test plugin APA citation";
const testPluginBibtexCitation = "Test plugin BibTeX citation";
const testExtensionApaCitation = "Test extension APA citation";

let jsPsych;
beforeEach(() => {
jsPsych = initJsPsych();
});
let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance;

describe("citing not using an array", () => {
test("citing nothing", () => {
const citation = jsPsych.getCitations();
console.log(citation);
expect(citation).toMatch(jsPsychApaCitation);
});
test("citing without input and with valid format", () => {
jsPsych.getCitations(null, "apa");
expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
});
test("citing without input and with invalid format", () => {
expect(() => jspsych.getCitations(null, "dummyTex")).toThrow("Unsupported citation format");
});
});
let JsPsych;

describe("citing using an array in different formats", () => {
test("citing empty array with APA format", () => {
jspsych.getCitations([], "apa");
expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
});
test("citing empty array with BibTeX format", () => {
jspsych.getCitations([], "bibtex");
expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychBibtexCitation);
});
test("citing empty array without format", () => {
jspsych.getCitations([]);
expect(consoleLogSpy.mock.calls).toHaveLength(1);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
});
test("citing one plugin with valid format in all caps", () => {
const citation = jsPsych.getCitations([TestPlugin], "APA");
expect(citation).toBe(jsPsychApaCitation + "\n" + testPluginApaCitation + "\n");
/**
* 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();
consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
});
test("citing with unsupported format", () => {
expect(() => jspsych.getCitations([TestPlugin], "DummyTex")).toThrow(
"Unsupported citation format"
);

afterEach(() => {
jest.restoreAllMocks();
});
});

describe("citing mix of valid plugins/extensions", () => {
test("citing a plugin", () => {
jspsych.getCitations([TestPlugin]);
expect(consoleLogSpy.mock.calls).toHaveLength(2);
// expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);
describe("citing not using an array", () => {
test("citing nothing", () => {
expect(() => jspsych.getCitations(null)).toThrow("Expected array of plugins/extensions");
});
test("citing without input and with invalid format", () => {
expect(() => jspsych.getCitations(null, "apa")).toThrow(
"Expected array of plugins/extensions"
);
});
});
test("citing a plugin in BibTeX", () => {
jspsych.getCitations([TestPlugin], "bibtex");
expect(consoleLogSpy.mock.calls).toHaveLength(2);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychBibtexCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginBibtexCitation);

describe("citing using an array in different formats", () => {
test("citing empty array with APA format", () => {
jspsych.getCitations([], "apa");
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided");

Check failure on line 53 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing empty array with APA format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:53:42)

Check failure on line 53 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing empty array with APA format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:53:42)

Check failure on line 53 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing empty array with APA format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:53:42)

Check failure on line 53 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing empty array with APA format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:53:42)
});
test("citing empty array with BibTeX format", () => {
jspsych.getCitations([], "bibtex");
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided");

Check failure on line 57 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing empty array with BibTeX format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:57:42)

Check failure on line 57 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing empty array with BibTeX format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:57:42)

Check failure on line 57 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing empty array with BibTeX format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:57:42)

Check failure on line 57 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing empty array with BibTeX format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:57:42)
});
test("citing empty array without format", () => {
jspsych.getCitations([]);
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided");

Check failure on line 61 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing empty array without format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:61:42)

Check failure on line 61 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing empty array without format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:61:42)

Check failure on line 61 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing empty array without format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:61:42)

Check failure on line 61 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing empty array without format

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:61:42)
});
test("citing one plugin with valid format in all caps", () => {
jspsych.getCitations([TestPlugin], "APA");
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);

Check failure on line 65 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing one plugin with valid format in all caps

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:65:41)

Check failure on line 65 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing one plugin with valid format in all caps

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:65:41)

Check failure on line 65 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing using an array in different formats › citing one plugin with valid format in all caps

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:65:41)

Check failure on line 65 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing using an array in different formats › citing one plugin with valid format in all caps

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:65:41)
});
test("citing with unsupported format", () => {
expect(() => jspsych.getCitations([TestPlugin], "DummyTex")).toThrow(
"Unsupported citation format"
);
});
});
test("citing multiple of the same plugins", () => {
jspsych.getCitations([TestPlugin, TestPlugin]);
expect(consoleLogSpy.mock.calls).toHaveLength(2);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);

describe("citing mix of valid plugins/extensions", () => {
test("citing a plugin", () => {
jspsych.getCitations([TestPlugin]);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);

Check failure on line 77 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing a plugin

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:77:41)

Check failure on line 77 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing a plugin

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:77:41)

Check failure on line 77 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing a plugin

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:77:41)

Check failure on line 77 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing a plugin

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:77:41)
});
test("citing a plugin in BibTeX", () => {
jspsych.getCitations([TestPlugin], "bibtex");
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginBibtexCitation);

Check failure on line 81 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing a plugin in BibTeX

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:81:41)

Check failure on line 81 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing a plugin in BibTeX

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:81:41)

Check failure on line 81 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing a plugin in BibTeX

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:81:41)

Check failure on line 81 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing a plugin in BibTeX

TypeError: Cannot read properties of undefined (reading '0') at Object.<anonymous> (tests/citations/citations.test.ts:81:41)
});
test("citing multiple plugins", () => {
jspsych.getCitations([TestPlugin, TestPlugin]);
expect(consoleLogSpy.mock.calls).toHaveLength(2);

Check failure on line 85 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing multiple plugins

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:85:40)

Check failure on line 85 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing multiple plugins

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:85:40)

Check failure on line 85 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing multiple plugins

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:85:40)

Check failure on line 85 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing multiple plugins

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:85:40)
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);
});
test("citing mix of plugins and extensions", () => {
jspsych.getCitations([TestPlugin, TestExtension]);
expect(consoleLogSpy.mock.calls).toHaveLength(2);

Check failure on line 91 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing mix of plugins and extensions

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:91:40)

Check failure on line 91 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing mix of plugins and extensions

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:91:40)

Check failure on line 91 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 18

citing mix of valid plugins/extensions › citing mix of plugins and extensions

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:91:40)

Check failure on line 91 in packages/jspsych/tests/citations/citations.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node.js 20

citing mix of valid plugins/extensions › citing mix of plugins and extensions

expect(received).toHaveLength(expected) Expected length: 2 Received length: 0 Received array: [] at Object.<anonymous> (tests/citations/citations.test.ts:91:40)
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testExtensionApaCitation);
});
});
test("citing mix of plugins and extensions", () => {
jspsych.getCitations([TestPlugin, TestExtension]);
expect(consoleLogSpy.mock.calls).toHaveLength(3);
expect(consoleLogSpy.mock.calls[0][0]).toBe(jsPsychApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testPluginApaCitation);
expect(consoleLogSpy.mock.calls[2][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);
});
});
});
}

0 comments on commit 466b44c

Please sign in to comment.