@bonniernews/fake-tool-api
Advanced tools
Comparing version 0.0.1 to 0.0.2
125
index.js
@@ -12,11 +12,8 @@ import nock from "nock"; | ||
let interceptor = () => {}; | ||
let interceptor = null; | ||
export function intercept(interceptFn) { | ||
interceptor = interceptFn; | ||
} | ||
let pubSubListener; | ||
export function init(toolApiBaseUrl, listener) { | ||
pubSubListener = listener; | ||
clear(); | ||
const mock = nock(toolApiBaseUrl); | ||
@@ -37,2 +34,6 @@ mock | ||
.persist() | ||
.post("/slug") // todo collisionresolver | ||
.query(true) | ||
.reply(interceptable(postPath)) | ||
.persist() | ||
.put(singleContentRegex) | ||
@@ -46,5 +47,58 @@ .reply(interceptable(put)) | ||
export function intercept(interceptFn) { | ||
interceptor = interceptFn; | ||
} | ||
export async function addContent(type, id, content, skipEvents) { | ||
addType(type); | ||
const existingContent = contentByType[type][id]; | ||
const sequenceNumber = existingContent?.sequenceNumber ? existingContent?.sequenceNumber + 1 : 1; | ||
content.updated = content.updated || new Date().toISOString(); | ||
contentByType[type][id] = { sequenceNumber, content }; | ||
if (!skipEvents && pubSubListener) await sendEvent(type, id, "published"); | ||
} | ||
export function addPath(path) { | ||
if (path.channels) { | ||
throw new Error("slug.channels is deprecated, use slug.channel instead"); | ||
} | ||
if (!path.publishTime) { | ||
path.publishTime = new Date(); | ||
} | ||
paths.push(path); | ||
} | ||
export function removePath(path) { | ||
paths = paths.filter((p) => !(p.channel === path.channelId && p.value === path.value && p.path === path.path)); | ||
} | ||
export async function removeContent(type, id) { | ||
delete contentByType[type][id]; | ||
await sendEvent(type, id, "unpublished"); | ||
} | ||
export function addType(type) { | ||
if (!contentByType[type]) { | ||
contentByType[type] = {}; | ||
} | ||
} | ||
export function peekContent(type, id) { | ||
return contentByType[type]?.[id]; | ||
} | ||
export function peekPaths() { | ||
return paths; | ||
} | ||
export function clear() { | ||
contentByType = {}; | ||
paths.length = 0; | ||
interceptor = null; | ||
} | ||
function interceptable(fn) { | ||
return function () { | ||
const intercepted = interceptor(this.method, ...arguments); | ||
const interceptorFn = interceptor || (() => {}); | ||
const intercepted = interceptorFn(this.method, ...arguments); | ||
if (intercepted) return intercepted; | ||
@@ -130,2 +184,3 @@ return fn.apply(this, arguments); | ||
function get(url) { | ||
const matches = url.match(singleContentRegex); | ||
@@ -177,3 +232,16 @@ const [ , type, id ] = matches || []; | ||
function postPath(url, body) { | ||
const { channel, value, valueType, publishTime } = body; | ||
paths.push({ | ||
path: body.desiredPath, | ||
channel, | ||
value, | ||
valueType, | ||
publishTime, | ||
}); | ||
return [ 200, "OK" ]; | ||
} | ||
function put(url, body) { | ||
const matches = url.match(singleContentRegex); | ||
@@ -189,3 +257,3 @@ const [ , type, id ] = matches || []; | ||
ofType[id] = body; | ||
addContent(type, id, body); | ||
@@ -211,15 +279,3 @@ return [ 200, body ]; | ||
export async function addContent(type, id, content, skipEvents) { | ||
addType(type); | ||
const existingContent = contentByType[type][id]; | ||
const sequenceNumber = existingContent?.sequenceNumber ? existingContent?.sequenceNumber + 1 : 1; | ||
content.updated = content.updated || new Date().toISOString(); | ||
contentByType[type][id] = { sequenceNumber, content }; | ||
if (!skipEvents) await sendEvent(type, id, "published"); | ||
} | ||
async function sendEvent(type, id, event) { | ||
const message = { | ||
@@ -231,3 +287,2 @@ id, | ||
id, | ||
// url: `${config.toolApi.baseUrl}/${type}/${id}`, | ||
})), | ||
@@ -238,31 +293,1 @@ attributes: {}, | ||
} | ||
export function addPath(path) { | ||
if (path.channels) { | ||
throw new Error("slug.channels is deprecated, use slug.channel instead"); | ||
} | ||
if (!path.publishTime) { | ||
path.publishTime = new Date(); | ||
} | ||
paths.push(path); | ||
} | ||
export function removePath(path) { | ||
paths = paths.filter((p) => !(p.channel === path.channelId && p.value === path.value && p.path === path.path)); | ||
} | ||
export async function removeContent(type, id) { | ||
delete contentByType[type][id]; | ||
await sendEvent(type, id, "unpublished"); | ||
} | ||
export function addType(type) { | ||
if (!contentByType[type]) { | ||
contentByType[type] = {}; | ||
} | ||
} | ||
export function clear() { | ||
contentByType = {}; | ||
paths.length = 0; | ||
} |
{ | ||
"name": "@bonniernews/fake-tool-api", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"type": "module", | ||
@@ -12,2 +12,5 @@ "description": "Mocked Bonnier News tool api, for use in automated tests", | ||
}, | ||
"scripts": { | ||
"test": "mocha test" | ||
}, | ||
"license": "MIT", | ||
@@ -17,3 +20,6 @@ "files": ["index.js"], | ||
"@bonniernews/eslint-config": "^1.1.0", | ||
"eslint": "^8.56.0" | ||
"chai": "^5.1.0", | ||
"eslint": "^8.56.0", | ||
"mocha": "^10.3.0", | ||
"node-fetch": "^3.3.2" | ||
}, | ||
@@ -20,0 +26,0 @@ "dependencies": { |
# Fake tool api | ||
Mock version of Bonnier News tool api, for use in automated tests. | ||
See 'test/index.spec.js' for example usage | ||
## Setup | ||
## Pubsub | ||
## Exmaples |
7565
247
5
5