Skip to content

Commit

Permalink
Handle learning track URL redirects (github#21613)
Browse files Browse the repository at this point in the history
* Handle redirects for learning tracks

* Test for learning track URL redirects

* Test latest Enterprise version

Co-authored-by: Rachael Sewell <rachmari@github.com>

Co-authored-by: Rachael Sewell <rachmari@github.com>
  • Loading branch information
rsese and rachmari authored Sep 22, 2021
1 parent e4c120d commit 27eaff7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion middleware/learning-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ export default async function learningTrack(req, res, next) {
const currentLearningTrack = { trackName }

const guidePath = getPathWithoutLanguage(getPathWithoutVersion(req.pagePath))
const guideIndex = track.guides.findIndex((path) => path === guidePath)
let guideIndex = track.guides.findIndex((path) => path === guidePath)

if (guideIndex < 0) {
// Also check if the learning track URL is now a redirect to the requested
// page, we still want to render the learning track banner in that case.
for (const redirect of req.context.page.redirect_from) {
track.guides.forEach((path, i) => {
if (path === redirect) {
guideIndex = i
}
})
}
}

if (guideIndex < 0) return noTrack()

Expand Down
12 changes: 12 additions & 0 deletions tests/rendering/learning-tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe.skip('navigation banner', () => {
})
})

test('render navigation banner when url is a redirect to a learning track URL', async () => {
const $ = await getDOM(
'/enterprise/admin/enterprise-management/enabling-automatic-update-checks?learn=upgrade_your_instance'
)
expect($('[data-testid=learning-track-nav]')).toHaveLength(1)
const $navLinks = $('[data-testid=learning-track-nav] a')
expect($navLinks).toHaveLength(1)
$navLinks.each((i, elem) => {
expect($(elem).attr('href')).toEqual(expect.stringContaining('?learn=upgrade_your_instance'))
})
})

test('does not include banner when url does not include `learn` param', async () => {
const $ = await getDOM(
'/en/actions/guides/setting-up-continuous-integration-using-workflow-templates'
Expand Down

0 comments on commit 27eaff7

Please sign in to comment.