diff --git a/index.js b/index.js index 876af5c..241bcf7 100644 --- a/index.js +++ b/index.js @@ -84,7 +84,6 @@ export function intercept(interceptFn) { // resets content an initialize basic types () export function resetContent() { initBasetypes(); - workingCopiesByType = {}; slugs = []; interceptor = () => { }; versionsMeta = {}; @@ -96,6 +95,8 @@ export function resetContent() { // Removes all base types (needed for a few very generic tests) export function clearBaseTypes() { types = {}; + workingCopiesByType = {}; + contentByType = {}; } export function addWorkingCopy(type, id, content) { @@ -135,20 +136,17 @@ export function removeSlug(slug) { && p.value === slug.value && p.path === slug.path)); } -export function addType(type) { +export function addType(type, allowRedefine = false) { if (!type.properties) { type.properties = {}; } - if (!types[type.name]) { - types[type.name] = mapType(type); - - } - if (!contentByType[type.name]) { - contentByType[type.name] = {}; - } - if (!workingCopiesByType[type.name]) { - workingCopiesByType[type.name] = {}; + if (types[type.name] && !allowRedefine) { + throw new Error(`Type ${type.name} is already defined`); } + + types[type.name] = mapType(type); + contentByType[type.name] = {}; + workingCopiesByType[type.name] = {}; } export function peekContent(type, id) { @@ -214,8 +212,7 @@ function interceptable(routeInterceptor, route) { } function initBasetypes() { - contentByType = { channel: {}, "publishing-group": {} }; - types = {}; + clearBaseTypes(); addType({ name: "channel", properties: { attributes: { type: "object", properties: { name: { type: "string" } } } }, @@ -227,6 +224,7 @@ function initBasetypes() { addType({ name: "article", properties: { attributes: { type: "object", properties: { name: { type: "string" }, headline: { type: "string" } } } }, + canHaveSlugs: true, }); } diff --git a/package-lock.json b/package-lock.json index 1a406a1..8955c35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@bonniernews/fake-tool-api", - "version": "1.2.0", + "version": "1.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@bonniernews/fake-tool-api", - "version": "1.2.0", + "version": "1.3.0", "license": "MIT", "dependencies": { "nock": "^13.5.3", @@ -16,7 +16,7 @@ "@bonniernews/eslint-config": "^1.1.0", "chai": "^5.1.0", "eslint": "^8.57.1", - "mocha": "^10.3.0", + "mocha": "^11.0.0", "node-fetch": "^3.3.2" } }, @@ -2968,10 +2968,11 @@ } }, "node_modules/mocha": { - "version": "10.7.3", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-10.7.3.tgz", - "integrity": "sha512-uQWxAu44wwiACGqjbPYmjo7Lg8sFrS3dQe7PP2FQI+woptP4vZXSMcfMyFL/e1yFEeEpV4RtyTpZROOKmxis+A==", + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-11.0.0.tgz", + "integrity": "sha512-9VQaK0N4YQ2F89Vy4wTIEyTm/Ggcv1PejfVeI82wOw0vBO6BjFyBGHCiNbl+wyHmgWDyFmHb2Yw1QlLaWzaEoA==", "dev": true, + "license": "MIT", "dependencies": { "ansi-colors": "^4.1.3", "browser-stdout": "^1.3.1", @@ -2999,7 +3000,7 @@ "mocha": "bin/mocha.js" }, "engines": { - "node": ">= 14.0.0" + "node": "^18.18.0 || ^20.9.0 || >=21.1.0" } }, "node_modules/mocha/node_modules/minimatch": { diff --git a/package.json b/package.json index 3e6ce93..25a21b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bonniernews/fake-tool-api", - "version": "1.2.0", + "version": "1.3.0", "type": "module", "description": "Mocked Bonnier News tool api, for use in automated tests", "main": "index.js", @@ -25,7 +25,7 @@ "@bonniernews/eslint-config": "^1.1.0", "chai": "^5.1.0", "eslint": "^8.57.1", - "mocha": "^10.3.0", + "mocha": "^11.0.0", "node-fetch": "^3.3.2" }, "dependencies": { diff --git a/test/index.spec.js b/test/index.spec.js index b5d3a77..f773122 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -12,7 +12,7 @@ describe("Fake tool api", () => { fakeToolApi.init(baseUrl, (msg) => { events.push(JSON.parse(msg.data)); }); - fakeToolApi.addType({ name: "article" }); + fakeToolApi.addType({ name: "article" }, true); events.length = 0; }); const id = randomUUID(); @@ -85,6 +85,29 @@ describe("Fake tool api", () => { }); + describe("#addType", () => { + beforeEach(() => { + fakeToolApi.clearBaseTypes(); + }); + + it("should let user know if a type has already been defined", () => { + fakeToolApi.addType({ name: "test" }); + expect(() => fakeToolApi.addType({ name: "test" })).to.throw("Type test is already defined"); + }); + + it("should allow type redefinitions if explicitly set", async () => { + fakeToolApi.addType({ name: "test" }); + fakeToolApi.addType({ + name: "test", + properties: { attributes: { type: "object", properties: { cool: { type: "string" } } } }, + }, true); + + const res = await fetch(`${baseUrl}/types`); + const types = await res.json(); + expect(types[0].properties.attributes.properties).to.have.property("cool"); + }); + }); + describe("PUT /content", () => { it("should reject non-uuid ids", async () => {