Skip to content

Commit

Permalink
Merge pull request #10 from liquid-labs/work-liquid-labs/question-and…
Browse files Browse the repository at this point in the history
…-answer/9

Add test for 'cookie' parameters
  • Loading branch information
zanerock committed Mar 17, 2023
2 parents 4e8b7f8 + 1227c69 commit 26e38a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/lib/questioner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const Questioner = class {
// We want source second so that we create a new object rather than modify source. We want 'value' last because
// the 'value' attached to the source is always a string, while the final value will have been converted by type.
// We could also use 'structuredClone', but Object.assign should be sufficient.
this.#results.push(Object.assign({}, source, { value }))
const result = Object.assign({}, source, { value })
delete result.mappings
this.#results.push(result)
}

async #askQuestion(q) {
Expand Down Expand Up @@ -66,8 +68,11 @@ const Questioner = class {
}

get(parameter) {
console.error(this.#results)
return this.#results.find((r) => r.parameter === parameter)?.value
return this.getResult(parameter)?.value
}

getResult(parameter) {
return this.#results.find((r) => r.parameter === parameter)
}

get interogationBundle() { return this.#interogationBundle } // TODO: clone
Expand Down
27 changes: 26 additions & 1 deletion src/lib/test/questioner.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global afterAll describe expect fail jest test */
/* global afterAll beforeAll describe expect fail jest test */
import * as fsPath from 'node:path'
import { spawn } from 'node:child_process'

import { stdin } from 'mock-stdin'

import {
badParameterIB,
cookieParameterIB,
noQuestionParameterIB,
noQuestionPromptIB,
simpleIB,
Expand Down Expand Up @@ -151,4 +152,28 @@ describe('Questioner', () => {
const questioner = new Questioner()
expect(() => { questioner.interogationBundle = ib }).toThrow(exceptionRe)
})

describe('cookie parameters', () => {
const questioner = new Questioner({ input })

beforeAll(async() => {
questioner.interogationBundle = cookieParameterIB

const qPromise = questioner.question()
input.send('yes\n')
await qPromise
})

test('are passed from questions', () =>
expect(questioner.getResult('IS_CLIENT').handling).toBe('bundle')
)

test('are passed from question maps', () =>
expect(questioner.getResult('ORG_COMMON_NAME').handling).toBe('bundle')
)

test('are passed from question maps', () =>
expect(questioner.getResult('TARGET_DEMO').handling).toBe('bundle')
)
})
})
10 changes: 10 additions & 0 deletions src/lib/test/test-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ simpleMapIB.mappings = structuredClone(commonMapping)
const simpleLocalMapIB = structuredClone(simpleIB)
simpleLocalMapIB.questions[0].mappings = structuredClone(commonMapping)

const cookieParameterIB = structuredClone(simpleIB)
cookieParameterIB.mappings = structuredClone(commonMapping)
cookieParameterIB.questions[0].mappings = structuredClone(commonMapping)
cookieParameterIB.mappings[0].maps[0].parameter = 'TARGET_DEMO'
cookieParameterIB.mappings[1].maps[0].parameter = 'TARGET_DEMO'
cookieParameterIB.questions[0].handling = 'bundle'
cookieParameterIB.mappings[0].maps[0].handling = 'bundle'
cookieParameterIB.questions[0].mappings[0].maps[0].handling = 'bundle'

const DO_YOU_LIKE_MILK = 'Do you like milk?'
const IS_THIS_THE_END = 'Is this the end?'
const conditionalQuestionIB = structuredClone(simpleIB)
Expand Down Expand Up @@ -62,6 +71,7 @@ export {
IS_THE_COMPANY_THE_CLIENT,
DO_YOU_LIKE_MILK,
IS_THIS_THE_END,
cookieParameterIB,
simpleIntQuestionIB,
simpleIB,
simpleMapIB,
Expand Down

0 comments on commit 26e38a0

Please sign in to comment.