Skip to content

Commit

Permalink
Merge pull request #85 from k6io/feat/custom-group-name-property
Browse files Browse the repository at this point in the history
[feat] Ability to specify group name without modifying id.
  • Loading branch information
legander authored Jul 12, 2021
2 parents e7084bd + 818f692 commit 42f08be
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/parse/page.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
const sleep = require('./sleep')

function page(node, result) {
function pageName(node) {
const nameParts = []

node.id && nameParts.push(node.id)
node.title && nameParts.push(node.title)
if (node.name) {
nameParts.push(node.name)
} else if (node.id) {
nameParts.push(node.id)
}

if (node.title) {
nameParts.push(node.title)
}

return nameParts.join(' - ')
}

function page(node, result) {
const spec = {
name: nameParts.join(' - '),
name: pageName(node),
}

if (node.comment) {
Expand Down
6 changes: 6 additions & 0 deletions src/validate/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ function validate(node, i, assay) {
`Invalid page title (${i}): must be string`
)
}
if (!empty(node.name) && typeof node.name !== 'string') {
throw new InvalidArchiveError(
{ name: 'InvalidPageName' },
`Invalid page name (${i}): must be string`
)
}
if (node.sleep && !Array.isArray(node.sleep)) {
throw new InvalidArchiveError(
{ name: 'InvalidPageSleep' },
Expand Down
48 changes: 48 additions & 0 deletions test/unit/parse/page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,54 @@ test('main', (t) => {
)
})

test('favor name before title', (t) => {
const result = makeResult()
page({ id: 'page_id', name: 'Page name', title: 'Page title' }, result)
t.deepEqual(
result.pages,
new Map([
[
'page_id',
{
name: 'Page name - Page title',
},
],
])
)
})

test('fallback to using id as name', (t) => {
const result = makeResult()
page({ id: 'page_id_1', title: 'Page title' }, result)

const result2 = makeResult()
page({ id: 'page_id_2', name: '', title: 'Page title' }, result2)

t.deepEqual(
result.pages,
new Map([
[
'page_id_1',
{
name: 'page_id_1 - Page title',
},
],
])
)

t.deepEqual(
result2.pages,
new Map([
[
'page_id_2',
{
name: 'page_id_2 - Page title',
},
],
])
)
})

test('comment', (t) => {
const result = makeResult()
page({ id: 'page1', title: 'Page 1', comment: 'Heavy load' }, result)
Expand Down

0 comments on commit 42f08be

Please sign in to comment.