diff --git a/server/index.js b/server/index.js index 1fac976a..95062ce3 100644 --- a/server/index.js +++ b/server/index.js @@ -82,7 +82,7 @@ app.use(pages) app.use(cache) // category pages will be cache busted when their last updated timestamp changes -app.use(categories) +app.use(categories.router) app.use(playlists) postload.forEach((middleware) => app.use(middleware)) diff --git a/server/routes/categories.js b/server/routes/categories.js index f714faaf..c9615758 100644 --- a/server/routes/categories.js +++ b/server/routes/categories.js @@ -9,7 +9,7 @@ const {getTemplates, sortDocs, stringTemplate} = require('../utils') const {parseUrl} = require('../urlParser') router.get('*', handleCategory) -module.exports = router +module.exports = {router, createRelatedList} const categories = getTemplates('categories') @@ -94,12 +94,10 @@ function prepareContextualData(data, url, breadcrumb, parent, slug) { const {children: siblings} = parent const {children} = data - const self = url.split('/').slice(-1)[0] // most of what we are doing here is preparing parents and siblings // we need the url and parent object, as well as the breadcrumb to do that - const siblingLinks = createRelatedList(siblings, self, `${url.split('/').slice(0, -1).join('/')}`) - const childrenLinks = createRelatedList(children || {}, self, url) - + const siblingLinks = createRelatedList(siblings) + const childrenLinks = createRelatedList(children || {}) // extend the breadcrumb with render data const parentLinks = url .split('/') @@ -121,9 +119,8 @@ function prepareContextualData(data, url, breadcrumb, parent, slug) { } } -function createRelatedList(slugs, self, baseUrl) { +function createRelatedList(slugs) { return Object.keys(slugs) - .filter((slug) => slug !== self) .map((slug) => { const {id} = slugs[slug] const {sort, prettyName, webViewLink, path: url, resourceType, tags} = getMeta(id) diff --git a/test/unit/categories.test.js b/test/unit/categories.test.js new file mode 100644 index 00000000..3ac9314a --- /dev/null +++ b/test/unit/categories.test.js @@ -0,0 +1,59 @@ +'use strict' +const sinon = require('sinon') +const list = require('../../server/list') +const {assert} = require('chai') + +describe('categories', () => { + const sampleChildren = { + 'test-duplicate-title-bug': { + nodeType: 'leaf', + prettyName: 'Test Duplicate Title Bug', + home: undefined, + homePrettyName: undefined, + id: '1Mk6PB_kprmtPaQ3UULjTQ4msv24qfo6yOqMBBxYIzuo', + breadcrumb: [[{}]], + sort: 'Test Duplicate Title Bug | team' + } + } + + afterEach(() => { + sinon.restore() + }) + + it('should not remove items matching the parent', () => { + const expected = [ + { + sort: 'Test Duplicate Title Bug | team', + name: 'Test Duplicate Title Bug', + editLink: 'https://docs.google.com/document/d/1Mk6PB_kprmtPaQ3UULjTQ4msv24qfo6yOqMBBxYIzuo/edit?usp=drivesdk', + resourceType: 'document', + url: '/test-duplicate-title-bug/test-duplicate-title-bug', + tags: ['team'] + } + ] + sinon.stub(list, 'getMeta').returns({ + id: '1Mk6PB_kprmtPaQ3UULjTQ4msv24qfo6yOqMBBxYIzuo', + name: 'Test Duplicate Title Bug | team', + parents: ['1DlHbZAobaiv6IkUP0O6FM6lAvufJZlPT'], + webViewLink: 'https://docs.google.com/document/d/1Mk6PB_kprmtPaQ3UULjTQ4msv24qfo6yOqMBBxYIzuo/edit?usp=drivesdk', + prettyName: 'Test Duplicate Title Bug', + tags: ['team'], + resourceType: 'document', + sort: 'Test Duplicate Title Bug | team', + slug: 'test-duplicate-title-bug', + folder: { + id: '1DlHbZAobaiv6IkUP0O6FM6lAvufJZlPT', + webViewLink: 'https://drive.google.com/drive/folders/1DlHbZAobaiv6IkUP0O6FM6lAvufJZlPT', + prettyName: 'Test Duplicate Title Bug', + tags: ['team'], + resourceType: 'folder', + sort: 'Test Duplicate Title Bug | team', + path: '/test-duplicate-title-bug' + }, + topLevelFolder: {path: '/', tags: []}, + path: '/test-duplicate-title-bug/test-duplicate-title-bug' + }) + const {createRelatedList} = require('../../server/routes/categories') // we have to import here to populate the local cache + assert.deepEqual(createRelatedList(sampleChildren), expected) + }) +})