From 21b8d898acdcd68aab3c4ca6494074d8e82d944d Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Fri, 19 Jan 2024 17:23:38 -0800 Subject: [PATCH] Add tests --- tests/unit/controllers/getStars.test.js | 2 +- .../models/constructPackageObjectFull.test.js | 64 +++++++++++++++++++ .../models/constructPackageObjectJSON.test.js | 23 +++++++ .../constructPackageObjectShort.test.js | 46 +++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 tests/unit/models/constructPackageObjectFull.test.js create mode 100644 tests/unit/models/constructPackageObjectJSON.test.js create mode 100644 tests/unit/models/constructPackageObjectShort.test.js diff --git a/tests/unit/controllers/getStars.test.js b/tests/unit/controllers/getStars.test.js index 97b634e9..e3e7f061 100644 --- a/tests/unit/controllers/getStars.test.js +++ b/tests/unit/controllers/getStars.test.js @@ -107,7 +107,7 @@ describe("Returns as expected", () => { return { ok: true, content: {} }; }, }; - localContext.utils = { + localContext.models = { constructPackageObjectShort: () => { return { item: "is_a_package" }; }, diff --git a/tests/unit/models/constructPackageObjectFull.test.js b/tests/unit/models/constructPackageObjectFull.test.js new file mode 100644 index 00000000..5713f773 --- /dev/null +++ b/tests/unit/models/constructPackageObjectFull.test.js @@ -0,0 +1,64 @@ +const pof = require("../../../src/models/constructPackageObjectFull.js"); +const schema = require("../../models/packageObjectFull.js").test; + +describe("Parses Data, as expected to be returned by the Database", () => { + + test("Correctly Parses normal data", async () => { + const data = { + pointer: "1234", + name: "test-package", + created: "2024-01-20T00:47:00.981Z", + updated: "2024-01-20T00:47:00.981Z", + creation_method: "Test Package", + downloads: "25", + data: { + name: "test-package", + owner: "pulsar-edit", + readme: "This is a readme!", + metadata: { + name: "test-package", + license: "MIT", + version: "1.0.0" + }, + releases: { latest: "1.0.0" }, + versions: { + "1.0.0": { + sha: "1234", + name: "test-package", + version: "1.0.0", + tarball_url: "https://nowhere.com" + } + }, + repository: { + url: "https://github.com/pulsar-edit/test-package", + type: "git" + }, + creation_method: "Test Package" + }, + owner: "pulsar-edit", + stargazers_count: "1", + versions: [ + { + id: 10, + meta: { + sha: "1234", + name: "test-package", + version: "1.0.0", + tarball_url: "https://nowhere.com" + }, + engine: { atom: "*" }, + semver: "1.0.0", + license: "MIT", + package: "1234", + hasGrammar: false, + hasSnippets: false, + supportedLanguages: null + } + ] + }; + + const parsed = await pof(data); + + expect(parsed).toMatchSchema(schema); + }); +}); diff --git a/tests/unit/models/constructPackageObjectJSON.test.js b/tests/unit/models/constructPackageObjectJSON.test.js new file mode 100644 index 00000000..033e79bc --- /dev/null +++ b/tests/unit/models/constructPackageObjectJSON.test.js @@ -0,0 +1,23 @@ +const poj = require("../../../src/models/constructPackageObjectJSON.js"); +const schema = require("../../models/packageObjectJSON.js").test; + +describe("Parses Data, as expected to be returned by the Database", () => { + + test("Correctly Parses normal data", async () => { + const data = { + semver: "1.0.0", + license: "MIT", + engines: { atom: "*" }, + meta: { + sha: "1234", + name: "package-test", + version: "1.0.0", + tarball_url: "https://nowhere.com" + } + }; + + const parsed = await poj(data); + + expect(parsed).toMatchSchema(schema); + }); +}); diff --git a/tests/unit/models/constructPackageObjectShort.test.js b/tests/unit/models/constructPackageObjectShort.test.js new file mode 100644 index 00000000..96005163 --- /dev/null +++ b/tests/unit/models/constructPackageObjectShort.test.js @@ -0,0 +1,46 @@ +const pos = require("../../../src/models/constructPackageObjectShort.js"); +const schema = require("../../models/packageObjectShort.js").test; + +describe("Parses Data, as expected to be returned by the Database", () => { + + test("Correctly Parses normal data", async () => { + const data = { + name: "test-package", + data: { + name: "test-package", + owner: "pulsar-edit", + readme: "This is a readme!", + metadata: { + name: "test-package", + license: "MIT", + version: "1.0.0" + }, + releases: { latest: "1.0.0" }, + versions: { + "1.0.0": { + sha: "1234", + name: "test-package", + version: "1.0.0", + tarball_url: "https://nowhere.com" + } + }, + repository: { + url: "https://github.com/pulsar-edit/test-package", + type: "git" + }, + creation_method: "Test Package" + }, + downloads: "0", + owner: "pulsar-edit", + stargazers_count: "0", + semver: "1.0.0", + created: "2024-01-20T00:46:57.014Z", + updated: "2024-01-20T00:46:57.014Z", + creation_method: "Test Package" + }; + + const parsed = await pos(data); + + expect(parsed).toMatchSchema(schema); + }); +});