Skip to content

Commit

Permalink
Add ability to have slugs to articles, and make user aware if a type …
Browse files Browse the repository at this point in the history
…has been defined
  • Loading branch information
wisko committed Nov 15, 2024
1 parent 493fd99 commit 63df2d5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
24 changes: 11 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export function intercept(interceptFn) {
// resets content an initialize basic types ()
export function resetContent() {
initBasetypes();
workingCopiesByType = {};
slugs = [];
interceptor = () => { };
versionsMeta = {};
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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" } } } },
Expand All @@ -227,6 +224,7 @@ function initBasetypes() {
addType({
name: "article",
properties: { attributes: { type: "object", properties: { name: { type: "string" }, headline: { type: "string" } } } },
canHaveSlugs: true,
});
}

Expand Down
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
25 changes: 24 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 63df2d5

Please sign in to comment.