Skip to content

Commit

Permalink
move out language dependent rendering tests (github#33761)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
  • Loading branch information
peterbe and heiskr authored Jan 5, 2023
1 parent 1c0a9ef commit 06eb34e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 90 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,8 @@ jobs:
env:
DIFF_FILE: get_diff_files.txt
CHANGELOG_CACHE_FILE_PATH: tests/fixtures/changelog-feed.json
# By default, when `process.env.NODE_ENV === 'test'` it forces the
# tests run only in English. The exception is the
# `tests/translations/` suite which needs all languages to be set up.
ENABLED_LANGUAGES: ${{ matrix.test-group == 'translations' && 'all' || '' }}
run: npm test -- tests/${{ matrix.test-group }}/
11 changes: 9 additions & 2 deletions lib/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,17 @@ const languages = {
}

if (process.env.ENABLED_LANGUAGES) {
if (process.env.ENABLED_LANGUAGES.toLowerCase() !== 'all') {
Object.keys(languages).forEach((code) => {
if (!process.env.ENABLED_LANGUAGES.includes(code)) delete languages[code]
})
console.log(`ENABLED_LANGUAGES: ${process.env.ENABLED_LANGUAGES}`)
}
} else if (process.env.NODE_ENV === 'test') {
// Unless explicitly set, when running tests default to just English
Object.keys(languages).forEach((code) => {
if (!process.env.ENABLED_LANGUAGES.includes(code)) delete languages[code]
if (code !== 'en') delete languages[code]
})
console.log(`ENABLED_LANGUAGES: ${process.env.ENABLED_LANGUAGES}`)
}

export const languageKeys = Object.keys(languages)
Expand Down
40 changes: 0 additions & 40 deletions tests/content/api-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,6 @@ describeIfElasticsearchURL('search v1 middleware', () => {
expect(res.headers['surrogate-key']).toBe('api-search:en')
})

test('basic search in Japanese', async () => {
const sp = new URLSearchParams()
// To see why this will work,
// see tests/content/fixtures/search-indexes/github-docs-dotcom-en-records.json
// which clearly has a record with the title "Foo"
sp.set('query', 'foo')
sp.set('language', 'ja')
const res = await get('/api/search/v1?' + sp)
expect(res.statusCode).toBe(200)
const results = JSON.parse(res.text)

expect(results.meta).toBeTruthy()
expect(results.meta.found.value).toBeGreaterThanOrEqual(1)
expect(results.meta.found.relation).toBeTruthy()
expect(results.meta.page).toBe(1)
expect(results.meta.size).toBeGreaterThanOrEqual(1)
expect(results.meta.took.query_msec).toBeGreaterThanOrEqual(0)
expect(results.meta.took.total_msec).toBeGreaterThanOrEqual(0)

// Might be empty but at least an array
expect(results.hits).toBeTruthy()
// The word 'foo' appears in more than 1 document in the fixtures.
expect(results.hits.length).toBeGreaterThanOrEqual(1)
// ...but only one has the word "foo" in its title so we can
// be certain it comes first.
const hit = results.hits[0]
// This specifically checks what we expect of version v1
expect(hit.url).toBe('/ja/foo')
expect(hit.title).toBe('フー')
expect(hit.breadcrumbs).toBe('fooing')

// Check that it can be cached at the CDN
expect(res.headers['set-cookie']).toBeUndefined()
expect(res.headers['cache-control']).toContain('public')
expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/)
expect(res.headers['surrogate-control']).toContain('public')
expect(res.headers['surrogate-control']).toMatch(/max-age=[1-9]/)
expect(res.headers['surrogate-key']).toBe('api-search:ja')
})

test('debug search', async () => {
const sp = new URLSearchParams()
sp.set('query', 'foo')
Expand Down
12 changes: 0 additions & 12 deletions tests/rendering/homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ describe('rendering the home page(s)', () => {
expect(products.length).toBe(1)
})

test('homepage in non-default language has product links', async () => {
const $ = await getDOM('/ja')
const products = $('[data-testid=product]')
expect(products.length).toBe(1)
})

test('homepage in non-default product', async () => {
const $ = await getDOM('/en/enterprise-cloud@latest')
const products = $('[data-testid=product]')
expect(products.length).toBe(1)
})

test('homepage in non-default product in non-default language', async () => {
const $ = await getDOM('/ja/enterprise-cloud@latest')
const products = $('[data-testid=product]')
expect(products.length).toBe(1)
})
})
43 changes: 12 additions & 31 deletions tests/rendering/release-notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { describe, jest } from '@jest/globals'

import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
import { get } from '../helpers/e2etest.js'
import languages from '../../lib/languages.js'
import Page from '../../lib/page.js'

// The English content page's `versions:` frontmatter is the source
// of (convenient) truth about which versions of this page is available.
const page = await Page.init({
basePath: 'content',
relativePath: 'admin/release-notes.md',
languageCode: 'en',
})

describe('server', () => {
jest.setTimeout(60 * 1000)

Expand All @@ -21,35 +28,9 @@ describe('server', () => {
expect(res.statusCode).toBe(200)
})

test('every version that has release-notes', async () => {
// The English content page's `versions:` frontmatter is the source
// of (convenient) truth about which versions of this page is available.
const page = await Page.init({
basePath: 'content',
relativePath: 'admin/release-notes.md',
languageCode: 'en',
})
const testLanguages = []
for (const [langCode, langObj] of Object.entries(languages)) {
if (!langObj.wip) {
testLanguages.push(langCode)
}
}

const statusCodes = await Promise.all(
page.applicableVersions.map(async (version) => {
return await Promise.all(
testLanguages.map(async (language) => {
const url = `/${language}/${version}/admin/release-notes`
const res = await get(url)
return [url, res.statusCode]
})
)
})
)
for (const [url, status] of statusCodes.flat()) {
expect(status, url).toBe(200)
}
expect.assertions(page.applicableVersions.length * testLanguages.length)
test.each(page.applicableVersions)('version %s that has release-notes', async (version) => {
const url = `/en/${version}/admin/release-notes`
const res = await get(url)
expect(res.statusCode).toBe(200)
})
})
60 changes: 59 additions & 1 deletion tests/translations/frame.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { languageKeys } from '../../lib/languages.js'
import { blockIndex } from '../../middleware/block-robots.js'
import { getDOMCached as getDOM } from '../helpers/e2etest.js'
import { get, getDOMCached as getDOM } from '../helpers/e2etest.js'
import Page from '../../lib/page.js'

const langs = languageKeys.filter((lang) => lang !== 'en')

Expand Down Expand Up @@ -56,3 +57,60 @@ describe('frame', () => {
)
})
})

describe('homepage', () => {
test.each(langs)('homepage in non-default product in non-default language', async (lang) => {
const $ = await getDOM(`/${lang}/enterprise-cloud@latest`)
const products = $('[data-testid=product]')
expect(products.length).toBe(1)
})

test.each(langs)('homepage in non-default language has product links', async (lang) => {
const $ = await getDOM(`/${lang}`)
const products = $('[data-testid=product]')
expect(products.length).toBe(1)
})
})

const page = await Page.init({
basePath: 'content',
relativePath: 'admin/release-notes.md',
languageCode: 'en',
})

describe('release notes', () => {
// Return an array of tuples for each language and each first version
// per plan. E.g. ...
// [
// ['ja', 'enterprise-server@3.8'],
// ['pt', 'enterprise-server@3.8'],
// ...
// ['ja', 'github-ae@latest'],
// ['pt', 'github-ae@latest'],
// ...
//
// This is useful because if we test every single individual version of
// every plan the test just takes way too long.
const getReleaseNotesVersionCombinations = (langs) => {
const combinations = []
const prefixes = []
for (const version of page.applicableVersions) {
const prefix = version.split('@')[0]
if (prefixes.includes(prefix)) {
continue
}
prefixes.push(prefix)
combinations.push(...langs.map((lang) => [lang, version]))
}
return combinations
}

test.each(getReleaseNotesVersionCombinations(langs))(
'latest release notes',
async (lang, version) => {
const url = `/${lang}/${version}/admin/release-notes`
const res = await get(url)
expect(res.statusCode).toBe(200)
}
)
})
8 changes: 4 additions & 4 deletions tests/unit/get-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { DataDirectory } from '../helpers/data-directory.js'
describe('get-data', () => {
let dd
const enDirBefore = languages.en.dir
const jaDirBefore = languages.ja.dir
// Only `en` is available in jest tests, so pretend we also have Japanese
languages.ja = Object.assign({}, languages.en, {})

beforeAll(() => {
dd = new DataDirectory({
Expand Down Expand Up @@ -65,7 +66,6 @@ describe('get-data', () => {
afterAll(() => {
dd.destroy()
languages.en.dir = enDirBefore
languages.ja.dir = jaDirBefore
})

test('getDataByLanguage variables English', () => {
Expand Down Expand Up @@ -217,7 +217,8 @@ front: >'matter
describe('get-data on corrupt translations', () => {
let dd
const enDirBefore = languages.en.dir
const jaDirBefore = languages.ja.dir
// Only `en` is available in jest tests, so pretend we also have Japanese
languages.ja = Object.assign({}, languages.en, {})

beforeAll(() => {
dd = new DataDirectory({
Expand Down Expand Up @@ -263,7 +264,6 @@ describe('get-data on corrupt translations', () => {
afterAll(() => {
dd.destroy()
languages.en.dir = enDirBefore
languages.ja.dir = jaDirBefore
})

test('getDataByLanguage on a corrupt .yml file', () => {
Expand Down

0 comments on commit 06eb34e

Please sign in to comment.