Skip to content

Commit

Permalink
fix: filter hidden pages from the currentProductTree (SidebarProduct) (
Browse files Browse the repository at this point in the history
…github#20404)

* fix some async test things

* allow eslint to parse top-level awaits

* fix: filter out hidden pages closer to SidebarProduct usage
  • Loading branch information
mikesurowiec authored Jul 20, 2021
1 parent 2d99aee commit 6bc50f7
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["next/babel"],
"plugins": [["styled-components", { "ssr": true }]]
"plugins": [["styled-components", { "ssr": true }], "@babel/plugin-syntax-top-level-await"]
}
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
ecmaVersion: 11,
requireConfigFile: 'false',
babelOptions: { configFile: './.babelrc' },
sourceType: 'module',
},
rules: {
'import/no-extraneous-dependencies': ['error', { packageDir: '.' }],
Expand Down
9 changes: 7 additions & 2 deletions components/context/MainContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pick from 'lodash/pick'

import type { BreadcrumbT } from 'components/Breadcrumbs'
import type { FeatureFlags } from 'components/hooks/useFeatureFlags'
import { ExcludesNull } from 'components/lib/ExcludesNull'

type ProductT = {
external: boolean
Expand Down Expand Up @@ -186,7 +187,11 @@ export const getMainContextFromRequest = (req: any): MainContextT => {
}

// only pull things we need from the product tree, and make sure there are default values instead of `undefined`
const getCurrentProductTree = (input: any): ProductTreeNode => {
const getCurrentProductTree = (input: any): ProductTreeNode | null => {
if (input.page.hidden) {
return null
}

return {
href: input.href,
renderedShortTitle: input.renderedShortTitle || '',
Expand All @@ -197,7 +202,7 @@ const getCurrentProductTree = (input: any): ProductTreeNode => {
title: input.page.title,
shortTitle: input.page.shortTitle || '',
},
childPages: (input.childPages || []).map(getCurrentProductTree),
childPages: (input.childPages || []).map(getCurrentProductTree).filter(ExcludesNull),
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/page-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export async function versionPages(obj, version, langCode, site) {

const versionedChildPages = await Promise.all(
obj.childPages
// Drop child pages that do not apply to the current version.
// Drop child pages that do not apply to the current version
.filter((childPage) => childPage.page.applicableVersions.includes(version))
// Version the child pages recursively.
.map((childPage) => versionPages(Object.assign({}, childPage), version, langCode, site))
Expand Down
4 changes: 2 additions & 2 deletions middleware/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import next from 'next'
const { NODE_ENV } = process.env
const isDevelopment = NODE_ENV === 'development'

const nextApp = next({ dev: isDevelopment })
export const nextApp = next({ dev: isDevelopment })
export const nextHandleRequest = nextApp.getRequestHandler()
nextApp.prepare()
await nextApp.prepare()

function renderPageWithNext(req, res, next) {
if (req.path.startsWith('/_next') && !req.path.startsWith('/_next/data')) {
Expand Down
23 changes: 15 additions & 8 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"@actions/github": "^5.0.0",
"@babel/core": "^7.14.3",
"@babel/eslint-parser": "^7.14.3",
"@babel/plugin-syntax-top-level-await": "^7.14.5",
"@babel/plugin-transform-runtime": "^7.14.3",
"@babel/preset-env": "^7.14.2",
"@graphql-inspector/core": "^2.5.0",
Expand Down
2 changes: 0 additions & 2 deletions tests/helpers/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@ export const getJSON = (helpers.getJSON = async function (route) {
const res = await helpers.get(route, { followRedirects: true })
return JSON.parse(res.text)
})

export default helpers
11 changes: 6 additions & 5 deletions tests/unit/early-access.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { jest } from '@jest/globals'
import xFs from 'fs'
import { stat } from 'fs/promises'
import path from 'path'
import { testViaActionsOnly } from '../helpers/conditional-runs.js'
import { getDOM } from '../helpers/supertest.js'
import got from 'got'
const fs = xFs.promises

jest.useFakeTimers()

describe('cloning early-access', () => {
testViaActionsOnly('the content directory exists', async () => {
const eaDir = path.join(process.cwd(), 'content/early-access')
expect(await fs.stat(eaDir)).toBeTruthy()
expect(await stat(eaDir)).toBeTruthy()
})

testViaActionsOnly('the data directory exists', async () => {
const eaDir = path.join(process.cwd(), 'data/early-access')
expect(await fs.stat(eaDir)).toBeTruthy()
expect(await stat(eaDir)).toBeTruthy()
})

testViaActionsOnly('the assets/images directory exists', async () => {
const eaDir = path.join(process.cwd(), 'assets/images/early-access')
expect(await fs.stat(eaDir)).toBeTruthy()
expect(await stat(eaDir)).toBeTruthy()
})
})

Expand Down

0 comments on commit 6bc50f7

Please sign in to comment.