-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3484 from jspsych/fix-build-citations-test
handle citation test edge case
- Loading branch information
Showing
10 changed files
with
188 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"jspsych": patch | ||
"@jspsych/config": patch | ||
--- | ||
|
||
Patches some edge cases for `getCitations` and the build process that reads CITATION.CFF files to include citation info |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./jest.cjs").makePackageConfig(__dirname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import fs from "node:fs"; | ||
|
||
import generateCitations from "../generateCitations"; | ||
|
||
// Mock filesystem | ||
jest.mock("node:fs"); | ||
jest.mock("app-root-path", () => ({ | ||
path: "/mock/root/path", | ||
})); | ||
|
||
describe("generateCitations", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const validCitationCff = ` | ||
cff-version: 1.2.0 | ||
message: Please cite this software using these metadata | ||
title: Test Software | ||
authors: | ||
- family-names: Doe | ||
given-names: John | ||
version: 1.0.0 | ||
date-released: 2023-01-01 | ||
`; | ||
|
||
const citationCffWithPreferred = ` | ||
cff-version: 1.2.0 | ||
message: Please cite this software using these metadata | ||
title: Test Software | ||
authors: | ||
- family-names: Doe | ||
given-names: John | ||
preferred-citation: | ||
title: Preferred Citation | ||
authors: | ||
- family-names: Smith | ||
given-names: Jane | ||
`; | ||
|
||
test("should generate citations when CITATION.cff exists in current directory", () => { | ||
fs.readFileSync.mockReturnValue(validCitationCff); | ||
|
||
const result = generateCitations(); | ||
|
||
expect(result).toHaveProperty("apa"); | ||
expect(result).toHaveProperty("bibtex"); | ||
expect(result.apa).not.toBe(""); | ||
expect(result.bibtex).not.toBe(""); | ||
}); | ||
|
||
test("should handle preferred-citation when present", () => { | ||
fs.readFileSync.mockReturnValue(citationCffWithPreferred); | ||
|
||
const result = generateCitations(); | ||
|
||
expect(result).toHaveProperty("apa"); | ||
expect(result).toHaveProperty("bibtex"); | ||
expect(result.apa.includes("Smith")).toBeTruthy(); | ||
}); | ||
|
||
test("should return empty strings when CITATION.cff is not found", () => { | ||
fs.readFileSync.mockImplementation(() => { | ||
throw new Error("File not found"); | ||
}); | ||
|
||
const result = generateCitations(); | ||
|
||
expect(result).toEqual({ | ||
apa: "", | ||
bibtex: "", | ||
}); | ||
}); | ||
|
||
test("should handle malformed CITATION.cff", () => { | ||
fs.readFileSync.mockReturnValue("invalid: yaml: content:"); | ||
|
||
const result = generateCitations(); | ||
|
||
expect(result).toEqual({ | ||
apa: "", | ||
bibtex: "", | ||
}); | ||
}); | ||
|
||
test("should remove newlines from citations", () => { | ||
fs.readFileSync.mockReturnValue(validCitationCff); | ||
|
||
const result = generateCitations(); | ||
|
||
expect(result.apa).not.toMatch(/\n/); | ||
expect(result.bibtex).not.toMatch(/\n/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,76 @@ | ||
import { initJsPsych } from "../../src"; | ||
import { TestExtension } from "../extensions/TestExtension"; | ||
import TestPlugin from "../TestPlugin"; | ||
|
||
const jsPsychApaCitation = | ||
"de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 "; | ||
const jsPsychBibtexCitation = | ||
'@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '; | ||
const jsPsychApaCitation = "Test base APA citation"; | ||
const jsPsychBibtexCitation = "Test base BibTeX citation"; | ||
const testPluginApaCitation = "Test plugin APA citation"; | ||
const testPluginBibtexCitation = "Test plugin BibTeX citation"; | ||
const testExtensionApaCitation = "Test extension APA citation"; | ||
|
||
let JsPsych; | ||
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. | ||
*/ | ||
beforeEach(() => { | ||
jspsych = initJsPsych(); | ||
(jspsych as any).citation = { | ||
apa: "Test base APA citation", | ||
bibtex: "Test base BibTeX citation", | ||
}; | ||
}); | ||
|
||
try { | ||
// Try to import built version | ||
JsPsych = require("../../dist/index").JsPsych; | ||
let jspsych: typeof JsPsych; | ||
|
||
beforeEach(() => { | ||
jspsych = new JsPsych(); | ||
describe("citing not using an array", () => { | ||
test("citing without input", () => { | ||
expect(jspsych.getCitations()).toBe(jsPsychApaCitation); | ||
}); | ||
|
||
describe("citing not using an array", () => { | ||
test("citing without input", () => { | ||
expect(jspsych.getCitations()).toBe(jsPsychApaCitation); | ||
}); | ||
test("citing null", () => { | ||
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 null", () => { | ||
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"); | ||
}); | ||
}); | ||
|
||
describe("citing using an array in different formats", () => { | ||
test("citing empty array with APA format", () => { | ||
expect(jspsych.getCitations([], "apa")).toBe(jsPsychApaCitation); | ||
}); | ||
test("citing empty array with BibTeX format", () => { | ||
expect(jspsych.getCitations([], "bibtex")).toBe(jsPsychBibtexCitation); | ||
}); | ||
test("citing empty array without format", () => { | ||
expect(jspsych.getCitations([])).toBe(jsPsychApaCitation); | ||
}); | ||
test("citing one plugin with valid format in all caps", () => { | ||
expect(jspsych.getCitations([TestPlugin], "APA")).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation | ||
); | ||
}); | ||
test("citing with unsupported format", () => { | ||
expect(() => jspsych.getCitations([TestPlugin], "DummyTex")).toThrow( | ||
"Unsupported citation format" | ||
); | ||
}); | ||
describe("citing using an array in different formats", () => { | ||
test("citing empty array with APA format", () => { | ||
expect(jspsych.getCitations([], "apa")).toBe(jsPsychApaCitation); | ||
}); | ||
test("citing empty array with BibTeX format", () => { | ||
expect(jspsych.getCitations([], "bibtex")).toBe(jsPsychBibtexCitation); | ||
}); | ||
test("citing empty array without format", () => { | ||
expect(jspsych.getCitations([])).toBe(jsPsychApaCitation); | ||
}); | ||
test("citing one plugin with valid format in all caps", () => { | ||
expect(jspsych.getCitations([TestPlugin], "APA")).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation | ||
); | ||
}); | ||
test("citing with unsupported format", () => { | ||
expect(() => jspsych.getCitations([TestPlugin], "DummyTex")).toThrow( | ||
"Unsupported citation format" | ||
); | ||
}); | ||
}); | ||
|
||
describe("citing mix of valid plugins/extensions", () => { | ||
test("citing a plugin", () => { | ||
expect(jspsych.getCitations([TestPlugin])).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation | ||
); | ||
}); | ||
test("citing a plugin in BibTeX", () => { | ||
expect(jspsych.getCitations([TestPlugin], "bibtex")).toBe( | ||
jsPsychBibtexCitation + "\n" + testPluginBibtexCitation | ||
); | ||
}); | ||
test("citing multiple plugins", () => { | ||
expect(jspsych.getCitations([TestPlugin, TestPlugin])).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation | ||
); | ||
}); | ||
test("citing mix of plugins and extensions", () => { | ||
expect(jspsych.getCitations([TestPlugin, TestExtension])).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation + "\n" + testExtensionApaCitation | ||
); | ||
}); | ||
describe("citing mix of valid plugins/extensions", () => { | ||
test("citing a plugin", () => { | ||
expect(jspsych.getCitations([TestPlugin])).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation | ||
); | ||
}); | ||
test("citing a plugin in BibTeX", () => { | ||
expect(jspsych.getCitations([TestPlugin], "bibtex")).toBe( | ||
jsPsychBibtexCitation + "\n" + testPluginBibtexCitation | ||
); | ||
}); | ||
test("citing multiple plugins", () => { | ||
expect(jspsych.getCitations([TestPlugin, TestPlugin])).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation | ||
); | ||
}); | ||
} 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); | ||
}); | ||
test("citing mix of plugins and extensions", () => { | ||
expect(jspsych.getCitations([TestPlugin, TestExtension])).toBe( | ||
jsPsychApaCitation + "\n" + testPluginApaCitation + "\n" + testExtensionApaCitation | ||
); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.