Skip to content

Commit

Permalink
add jest tests for citation tool
Browse files Browse the repository at this point in the history
  • Loading branch information
cherriechang committed Nov 1, 2024
1 parent 9d5215e commit f0bc8ae
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 1,005 deletions.
1,059 changes: 91 additions & 968 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import fs from "node:fs";
import { Cite } from "@citation-js/core";
import yaml from "yaml";

export default function generateCitation() {
/**
* Generate citation data from CITATION.cff file
* Currently supported formats: APA, BibTeX
*
* @returns {Object} - Object containing APA and BibTeX formatted citation data
*/
export default function generateCitations() {
let preferredCitation = false;

// Try to find CITATION.cff file and look for preferred-citation
Expand All @@ -31,7 +37,7 @@ export default function generateCitation() {
return { apa: "", bibtex: "" };
}

// Try to convert CITATION.cff to APA string
// Convert CITATION.cff to APA string
const citationApa = (() => {
try {
const apaCite = new Cite(citationCff);
Expand All @@ -48,7 +54,7 @@ export default function generateCitation() {
}
})();

// Try to convert CITATION.cff to bibtex string
// Convert CITATION.cff to BibTeX string
const citationBibtex = (() => {
try {
const bibtexCite = new Cite(citationCff);
Expand All @@ -60,7 +66,7 @@ export default function generateCitation() {
});
return citationBibtex;
} catch (error) {
console.log(`Error converting CITATION.cff to BibTex string: ${error.message}`);
console.log(`Error converting CITATION.cff to BibTeX string: ${error.message}`);
return null;
}
})();
Expand Down
9 changes: 5 additions & 4 deletions packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
},
"homepage": "https://www.jspsych.org/latest/developers/configuration",
"dependencies": {
"@citation-js/core": "^0.7.14",
"@citation-js/plugin-bibtex": "^0.7.14",
"@citation-js/plugin-csl": "^0.7.14",
"@citation-js/plugin-software-formats": "^0.6.1",
"@rollup/plugin-commonjs": "26.0.1",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-replace": "^6.0.1",
"@sucrase/jest-plugin": "3.0.0",
"@types/gulp": "4.0.17",
"@types/jest": "29.5.8",
"@citation-js/core": "^0.7.14",
"@citation-js/plugin-bibtex": "^0.7.14",
"@citation-js/plugin-csl": "^0.7.14",
"@citation-js/plugin-software-formats": "^0.6.1",
"alias-hq": "6.2.4",
"canvas": "^2.11.2",
"esbuild": "0.23.1",
Expand All @@ -60,6 +60,7 @@
"rollup": "4.21.2",
"rollup-plugin-dts": "6.1.1",
"rollup-plugin-esbuild": "6.1.1",
"rollup-plugin-modify": "^3.0.0",
"rollup-plugin-node-externals": "7.1.3",
"sucrase": "3.34.0",
"tslib": "2.6.2",
Expand Down
30 changes: 12 additions & 18 deletions packages/config/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import path from "path";

import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import { defineConfig } from "rollup";
import dts from "rollup-plugin-dts";
import esbuild from "rollup-plugin-esbuild";
import modify from "rollup-plugin-modify";
import externals from "rollup-plugin-node-externals";
import ts from "typescript";

import generateCitation from "./generateCitation.js";
import generateCitations from "./generateCitations.js";

const getTsCompilerOptions = () => {
const cwd = process.cwd();
Expand Down Expand Up @@ -41,7 +41,7 @@ const makeConfig = ({
...outputOptions,
};

const citationData = generateCitation();
const citationData = generateCitations();

/** @type{import("rollup-plugin-esbuild").Options} */
const esBuildPluginOptions = {
Expand Down Expand Up @@ -76,11 +76,9 @@ const makeConfig = ({
input,
plugins: [
externals(),
replace({
values: {
__APACITATION__: citationData.apa,
__BIBTEXCITATION__: citationData.bibtex,
},
modify({
find: /'__CITATIONS__'/g,
replace: JSON.stringify(citationData, null, 2),
}),
esbuild({ ...esBuildPluginOptions, target: "node18" }),
commonjs(commonjsPluginOptions),
Expand All @@ -107,11 +105,9 @@ const makeConfig = ({
input,
plugins: [
externals({ deps: false }),
replace({
values: {
__APACITATION__: citationData.apa,
__BIBTEXCITATION__: citationData.bibtex,
},
modify({
find: /'__CITATIONS__'/g,
replace: JSON.stringify(citationData, null, 2),
}),
resolve({ preferBuiltins: false }),
esbuild({ ...esBuildPluginOptions, target: "esnext" }),
Expand All @@ -132,11 +128,9 @@ const makeConfig = ({
input,
plugins: [
externals({ deps: false }),
replace({
values: {
__APACITATION__: citationData.apa,
__BIBTEXCITATION__: citationData.bibtex,
},
modify({
find: /'__CITATIONS__'/g,
replace: JSON.stringify(citationData, null, 2),
}),
resolve({ preferBuiltins: false }),
esbuild({ ...esBuildPluginOptions, target: "es2015", minify: true }),
Expand Down
47 changes: 47 additions & 0 deletions packages/extension-mouse-tracking/CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cff-version: "1.2.0"
authors:
- family-names: Leeuw
given-names: Joshua R.
name-particle: de
orcid: "https://orcid.org/0000-0003-4815-2364"
- family-names: Gilbert
given-names: Rebecca A.
orcid: "https://orcid.org/0000-0003-4574-7792"
- family-names: Luchterhandt
given-names: Björn
orcid: "https://orcid.org/0000-0002-9225-2787"
contact:
- family-names: Leeuw
given-names: Joshua R.
name-particle: de
orcid: "https://orcid.org/0000-0003-4815-2364"
doi: 10.5281/zenodo.7702307
message: If you use this software, please cite our article in the
Journal of Open Source Software.
preferred-citation:
authors:
- family-names: Leeuw
given-names: Joshua R.
name-particle: de
orcid: "https://orcid.org/0000-0003-4815-2364"
- family-names: Gilbert
given-names: Rebecca A.
orcid: "https://orcid.org/0000-0003-4574-7792"
- family-names: Luchterhandt
given-names: Björn
orcid: "https://orcid.org/0000-0002-9225-2787"
date-published: 2023-05-11
doi: 10.21105/joss.05351
issn: 2475-9066
issue: 85
journal: Journal of Open Source Software
publisher:
name: Open Journals
start: 5351
title: "jsPsych: Enabling an Open-Source Collaborative Ecosystem of
Behavioral Experiments"
type: article
url: "https://joss.theoj.org/papers/10.21105/joss.05351"
volume: 8
title: "jsPsych: Enabling an Open-Source Collaborative Ecosystem of
Behavioral Experiments"
1 change: 1 addition & 0 deletions packages/extension-mouse-tracking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class MouseTrackingExtension implements JsPsychExtension {
},
},
},
citations: "__CITATIONS__",
};

constructor(private jsPsych: JsPsych) {}
Expand Down
2 changes: 1 addition & 1 deletion packages/jspsych/src/ExtensionManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Class } from "type-fest";

import { TestExtension } from "../tests/extensions/test-extension";
import { TestExtension } from "../tests/extensions/TestExtension";
import { ExtensionManager, ExtensionManagerDependencies } from "./ExtensionManager";
import { JsPsych } from "./JsPsych";
import { JsPsychExtension } from "./modules/extensions";
Expand Down
26 changes: 21 additions & 5 deletions packages/jspsych/src/JsPsych.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,28 @@ export class JsPsych {
}

getCitations(
plugins: Array<Class<JsPsychPlugin<any>> | JsPsychExtension>,
format: "apa" | "bibtex"
plugins: Array<Class<JsPsychPlugin<any>> | Class<JsPsychExtension>>,
format?: string
) {
plugins.map((plugin) => {
let pluginCitation = plugin["info"].citation;
});
format = format ? format.toLowerCase() : "apa";
// Check if plugins is an array
if (!Array.isArray(plugins)) {
throw new Error("Expected array of plugins/extensions");
}
// Check if array is empty
else if (plugins.length == 0) {
console.warn("No plugins/extensions provided");
return;
}
// Check if format is supported
else if (!Object.keys(plugins[0]["info"].citations).includes(format)) {
throw new Error("Unsupported citation format");
} else {
plugins.map((plugin) => {
let pluginCitations = plugin["info"].citations;
console.log(format == "apa" ? `${pluginCitations.apa}` : `${pluginCitations.bibtex}`);
});
}
}

get extensions() {
Expand Down
1 change: 1 addition & 0 deletions packages/jspsych/src/modules/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface JsPsychExtensionInfo {
name: string;
version?: string;
data?: ParameterInfos;
citations?: Record<string, string> | string;
}

export interface JsPsychExtension {
Expand Down
1 change: 1 addition & 0 deletions packages/jspsych/src/modules/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export interface PluginInfo {
version?: string;
parameters: ParameterInfos;
data?: ParameterInfos;
citations?: Record<string, string> | string;
}

export interface JsPsychPlugin<I extends PluginInfo> {
Expand Down
4 changes: 4 additions & 0 deletions packages/jspsych/tests/TestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const testPluginInfo = <const>{
version: "0.0.1",
parameters: {},
data: {},
citations: {
apa: "Test plugin APA citation",
bibtex: "Test plugin BibTeX citation",
},
};

class TestPlugin implements JsPsychPlugin<typeof testPluginInfo> {
Expand Down
77 changes: 77 additions & 0 deletions packages/jspsych/tests/citations/citations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { JsPsych } from "../../src/JsPsych";
import { TestExtension } from "../extensions/TestExtension";
import TestPlugin from "../TestPlugin";

const testPluginApaCitation = "Test plugin APA citation";
const testPluginBibtexCitation = "Test plugin BibTeX citation";
const testExtensionApaCitation = "Test extension APA citation";

let jspsych: JsPsych;
let consoleLogSpy: jest.SpyInstance;
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
jspsych = new JsPsych();
consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
});

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

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");
});
});

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");
});
test("citing empty array with BibTeX format", () => {
jspsych.getCitations([], "bibtex");
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided");
});
test("citing empty array without format", () => {
jspsych.getCitations([]);
expect(consoleWarnSpy.mock.calls[0][0]).toBe("No plugins/extensions provided");
});
test("citing one plugin with valid format in all caps", () => {
jspsych.getCitations([TestPlugin], "APA");
expect(consoleLogSpy.mock.calls[0][0]).toBe(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", () => {
jspsych.getCitations([TestPlugin]);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);
});
test("citing a plugin in BibTeX", () => {
jspsych.getCitations([TestPlugin], "bibtex");
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginBibtexCitation);
});
test("citing multiple plugins", () => {
jspsych.getCitations([TestPlugin, TestPlugin]);
expect(consoleLogSpy.mock.calls).toHaveLength(2);
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);
expect(consoleLogSpy.mock.calls[0][0]).toBe(testPluginApaCitation);
expect(consoleLogSpy.mock.calls[1][0]).toBe(testExtensionApaCitation);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export class TestExtension implements JsPsychExtension {
name: "test",
version: "0.0.1",
data: {},
citations: {
apa: "Test extension APA citation",
bibtex: "Test extension BibTeX citation",
},
};

constructor(private jsPsych: JsPsych) {}
Expand Down
2 changes: 1 addition & 1 deletion packages/jspsych/tests/extensions/extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response";
import { pressKey, startTimeline } from "@jspsych/test-utils";

import { JsPsych, initJsPsych } from "../../src";
import { TestExtension } from "./test-extension";
import { TestExtension } from "./TestExtension";

jest.useFakeTimers();

Expand Down
5 changes: 1 addition & 4 deletions packages/plugin-html-keyboard-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ const info = <const>{
type: ParameterType.STRING,
},
},
citation: {
apa: "__APACITATION__",
bibtex: "__BIBTEXCITATION__",
},
citations: "__CITATIONS__",
};

type Info = typeof info;
Expand Down
Loading

0 comments on commit f0bc8ae

Please sign in to comment.