Skip to content

Commit

Permalink
Password (#113)
Browse files Browse the repository at this point in the history
* improve password logic

* improve logic

* add answer watcher

* add test

* add test

* set timeout of busy indicator to 1 sec

* cr fixes
  • Loading branch information
slavik-lvovsky authored Jan 19, 2020
1 parent 9794f81 commit 970e208
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
],
coverageThreshold: {
"global": {
"branches": 86,
"branches": 87,
"functions": 98,
"lines": 95,
"statements": 95
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,23 @@ export default {
}
},
methods: {
async updateQuestionsFromIndex(questionIndex) {
async updateQuestionsFromIndex(changedQuestionIndex) {
const questions = _.get(this, "currentPrompt.questions", []);
const relevantQuestionsToUpdate = _.slice(questions, questionIndex)
const relevantQuestionsToUpdate = _.slice(questions, changedQuestionIndex)
let showBusy = true
const that = this
let questionIndex = changedQuestionIndex - 1;
const finished = relevantQuestionsToUpdate.reduce((p, question) => {
return p.then(() => that.updateQuestion(question))
questionIndex++;
return p.then(() => that.updateQuestion(question, questionIndex, changedQuestionIndex))
}, Promise.resolve());
setTimeout(() => {
if (showBusy) {
that.showBusyIndicator = true
}
}, 500)
}, 1000)
await finished
showBusy = false
Expand All @@ -186,7 +188,7 @@ export default {
_.isEmpty(this.prompts) ||
(this.currentPrompt.status === PENDING && !this.isDone);
},
async updateQuestion(question) {
async updateQuestion(question, questionIndex, changedQuestionIndex) {
const newAnswers = this.currentPrompt.answers
try {
if (question.when === FUNCTION) {
Expand Down Expand Up @@ -215,6 +217,9 @@ export default {
question.answer = question.default;
}
}
if (question.type === "password" && questionIndex !== changedQuestionIndex) {
question.answer = undefined;
}
if (question._message === FUNCTION) {
question.message = await this.rpc.invoke("evaluateMethod", [
[newAnswers],
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/components/QuestionTypes/QuestionInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ export default {
return type === "input" ? "text" : type;
}
},
watch: {
"currentQuestion.answer": {
handler(currentAnswer) {
this.text = currentAnswer;
}
}
},
methods: {
onChange() {
const currentValue = _.isEmpty(this.text) ? _.get(this.currentQuestion, "default") : this.text;
Expand Down
26 changes: 23 additions & 3 deletions frontend/tests/App.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('App.vue', () => {
}
wrapper.vm.prompts = [{
questions: [{
name: 'validateQ', validate: '__Function', answer: 'validateAnswer', isWhen: true, doNotShow: false
name: 'validateQ', validate: '__Function', answer: 'validateAnswer', isWhen: true
}],
answers: {}
}]
Expand All @@ -175,7 +175,7 @@ describe('App.vue', () => {
}
wrapper.vm.prompts = [{
questions: [{
name: 'validateQ', validate: '__Function', answer: 'validateAnswer', isWhen: true, doNotShow: false
name: 'validateQ', validate: '__Function', answer: 'validateAnswer', isWhen: true
}],
answers: {}
}]
Expand All @@ -197,7 +197,7 @@ describe('App.vue', () => {
}
wrapper.vm.prompts = [{
questions: [{
name: 'validateQ', validate: '__Function', answer: 'validateAnswer', isWhen: true, doNotShow: false
name: 'validateQ', validate: '__Function', answer: 'validateAnswer', isWhen: true
}],
answers: {}
}]
Expand All @@ -212,6 +212,26 @@ describe('App.vue', () => {

invokeSpy.mockRestore();
})

test('invoke for question that comes before question of type password', async () => {
wrapper.vm.rpc = {
invoke: jest.fn().mockResolvedValue()
}
wrapper.vm.prompts = [{
questions: [{
name: 'serviceQ', validate: '__Function', answer: 'serviceAnswer', isWhen: true
}, {
name: 'passwordQ', validate: '__Function', answer: "somePassword", isWhen: true, type: "password"
}],
answers: {}
}]
wrapper.vm.generatorName = "testGen";
wrapper.vm.promptIndex = 0;

await wrapper.vm.updateQuestionsFromIndex(0)

expect(wrapper.vm.prompts[0].questions[1].answer).toBeUndefined()
})
})

describe('setQuestionProps - method', () => {
Expand Down
28 changes: 28 additions & 0 deletions frontend/tests/components/QuestionTypes/QuestionInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ describe('QuestionInput.vue', () => {
})
})

describe('currentQuestion.answe - watch', () => {
test('current answer is undefined', () => {
wrapper = initComponent(QuestionInput, {
currentQuestion: {
type: 'input', default: 'testDefault', answer: 'testAnswer'
},
updateQuestionsFromIndex: () => {}
})

wrapper.vm.$data.text = ''
wrapper.vm.$options.watch["currentQuestion.answer"].handler.call(wrapper.vm, undefined)
expect(wrapper.vm.$data.text).toBeUndefined()
})

test('current answer is defined', () => {
wrapper = initComponent(QuestionInput, {
currentQuestion: {
type: 'input', default: 'testDefault', answer: 'testAnswer'
},
updateQuestionsFromIndex: () => {}
})

wrapper.vm.$data.text = ''
wrapper.vm.$options.watch["currentQuestion.answer"].handler.call(wrapper.vm, "test")
expect(wrapper.vm.$data.text).toBe("test")
})
})

describe('text - method', () => {
test('text size is 0', async () => {
wrapper = initComponent(QuestionInput, {
Expand Down

0 comments on commit 970e208

Please sign in to comment.