From ffbee6c82c5b9ff1ce0d26ad75b03443e871f589 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 14 Mar 2024 12:44:45 -0400 Subject: [PATCH] Added some unit tests --- ghost/core/core/frontend/helpers/get.js | 2 + .../test/unit/frontend/helpers/get.test.js | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/ghost/core/core/frontend/helpers/get.js b/ghost/core/core/frontend/helpers/get.js index 53e362e6b6a7..ad0bcee88cec 100644 --- a/ghost/core/core/frontend/helpers/get.js +++ b/ghost/core/core/frontend/helpers/get.js @@ -364,3 +364,5 @@ module.exports = async function get(resource, options) { }; module.exports.async = true; + +module.exports.optimizeFilterCacheablity = optimizeFilterCacheablity; diff --git a/ghost/core/test/unit/frontend/helpers/get.test.js b/ghost/core/test/unit/frontend/helpers/get.test.js index 5ecfa6a586f4..9b5bf7ca0019 100644 --- a/ghost/core/test/unit/frontend/helpers/get.test.js +++ b/ghost/core/test/unit/frontend/helpers/get.test.js @@ -39,6 +39,45 @@ describe('{{#get}} helper', function () { sinon.restore(); }); + describe('cacheablity optimisation', function () { + it('Ignores non posts', function () { + const apiOptions = { + filter: 'id:-abcdef1234567890abcdef12' + }; + const { + options, + parseResult + } = get.optimizeFilterCacheablity('not-posts', apiOptions); + assert.equal(options.filter, 'id:-abcdef1234567890abcdef12'); + assert.deepEqual(parseResult({not: 'modified'}), {not: 'modified'}); + }); + it('Changes the filter for simple id negations', function () { + const apiOptions = { + filter: 'id:-abcdef1234567890abcdef12', + limit: 1 + }; + const { + options, + parseResult + } = get.optimizeFilterCacheablity('posts', apiOptions); + assert.equal(options.filter, 'id:-null'); + assert.deepEqual(parseResult({ + posts: [{ + id: 'abcdef1234567890abcdef12' + }, { + id: '1234567890abcdef12345678' + }] + }), { + posts: [{ + id: '1234567890abcdef12345678' + }], + meta: { + cacheablityOptimisation: true + } + }); + }); + }); + describe('context preparation', function () { const meta = {pagination: {}};