Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 55 additions & 37 deletions spec/src/modules/quizzes.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
return quizzes.getQuizNextQuestion(validQuizId, {}).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('next_question').to.be.an('object');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res.next_question.id).to.equal(1);
expect(res.next_question.options[0].id).to.equal(1);
expect(fetchSpy).to.have.been.called;
Expand All @@ -84,29 +85,34 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
return quizzes.getQuizNextQuestion(validQuizId, { section }).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('next_question').to.be.an('object');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res.next_question.id).to.equal(1);
expect(res.next_question.options[0].id).to.equal(1);
expect(requestedUrlParams).to.have.property('section').to.equal(section);
});
});

it('Should return a result provided a valid apiKey, quizId and versionId', () => {
const versionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d';
it('Should return a result provided a valid apiKey, quizId and quizVersionId, quizSessionId', () => {
const quizVersionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d';
const quizSessionId = '123;';
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return quizzes.getQuizNextQuestion(validQuizId, { versionId }).then((res) => {
return quizzes.getQuizNextQuestion(validQuizId, { quizVersionId, quizSessionId }).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('version_id').to.be.an('string').to.equal(versionId);
expect(res).to.have.property('quiz_version_id').to.be.an('string').to.equal(quizVersionId);
expect(res).to.have.property('next_question').to.be.an('object');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res.next_question.id).to.equal(1);
expect(res.next_question.options[0].id).to.equal(1);
expect(requestedUrlParams).to.have.property('version_id').to.equal(versionId);
expect(requestedUrlParams).to.have.property('quiz_version_id').to.equal(quizVersionId);
expect(requestedUrlParams).to.have.property('quiz_session_id').to.equal(quizSessionId);

});
});

Expand All @@ -121,8 +127,9 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
return quizzes.getQuizNextQuestion(validQuizId, {}).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('next_question').to.be.an('object');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res.next_question.id).to.equal(1);
expect(res.next_question.options[0].id).to.equal(1);
expect(requestedUrlParams).to.have.property('ui').to.equal(userId);
Expand All @@ -140,8 +147,9 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
return quizzes.getQuizNextQuestion(validQuizId, {}).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('next_question').to.be.an('object');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res.next_question.id).to.equal(1);
expect(res.next_question.options[0].id).to.equal(1);
expect(requestedUrlParams).to.have.property('us').to.deep.equal(segments);
Expand All @@ -155,37 +163,38 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
});

return quizzes.getQuizNextQuestion(validQuizId, { answers: validAnswers }).then((res) => {
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('next_question').to.be.an('object');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res.next_question.id).to.equal(4);
});
});

it('Should be rejected if an invalid quizId is provided', () => {
it('Should be rejected if an invalid quizVersionId is provided', () => {
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return expect(quizzes.getQuizNextQuestion('invalidQuizId', {})).to.eventually.be.rejected;
return expect(quizzes.getQuizNextQuestion(validQuizId, { quizVersionId: 'foo' })).to.eventually.be.rejected;
});

it('Should be rejected if no quizId is provided', () => {
it('Should be rejected if an invalid quizId is provided', () => {
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return expect(quizzes.getQuizNextQuestion(null, {})).to.eventually.be.rejected;
return expect(quizzes.getQuizNextQuestion('invalidQuizId', {})).to.eventually.be.rejected;
});

it('Should be rejected if an invalid versionId is provided', () => {
it('Should be rejected if no quizId is provided', () => {
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return expect(quizzes.getQuizNextQuestion(validQuizId, { versionId: 'foo' })).to.eventually.be.rejected;
return expect(quizzes.getQuizNextQuestion(null, {})).to.eventually.be.rejected;
});

if (!skipNetworkTimeoutTests) {
Expand Down Expand Up @@ -231,8 +240,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {

expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(res.response).to.have.property('results').to.be.an('array');
Expand All @@ -257,30 +266,35 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(requestedUrlParams).to.have.property('section').to.equal(section);
});
});

it('Should return a result provided a valid apiKey, quizId and versionId', () => {
const versionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d';
it('Should return a result provided a valid apiKey, quizId, quizVersionId and quizSessionId', () => {
const quizVersionId = 'e03210db-0cc6-459c-8f17-bf014c4f554d';
const quizSessionId = '12345';
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return quizzes.getQuizResults(validQuizId, { answers: validAnswers, versionId }).then((res) => {
// eslint-disable-next-line max-len
return quizzes.getQuizResults(validQuizId, { answers: validAnswers, quizVersionId, quizSessionId }).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string').to.equal(quizVersionId);
expect(res).to.have.property('quiz_session_id').to.be.an('string').to.equal(quizSessionId);
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(requestedUrlParams).to.have.property('version_id').to.equal(versionId);
expect(requestedUrlParams).to.have.property('quiz_version_id').to.equal(quizVersionId);
expect(requestedUrlParams).to.have.property('quiz_session_id').to.equal(quizSessionId);
});
});

Expand All @@ -297,10 +311,11 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {

expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;

expect(requestedUrlParams).to.have.property('ui').to.equal(userId);
});
});
Expand All @@ -318,8 +333,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {

expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(requestedUrlParams).to.have.property('us').to.deep.equal(segments);
Expand All @@ -339,7 +354,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(requestedUrlParams).to.have.property('page').to.equal(page.toString());
Expand All @@ -359,7 +375,8 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(requestedUrlParams).to.have.property('num_results_per_page').to.equal(resultsPerPage.toString());
Expand All @@ -379,39 +396,40 @@ describe(`ConstructorIO - Quizzes${bundledDescriptionSuffix}`, () => {
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
expect(res).to.have.property('result_id').to.be.an('string');
expect(res).to.have.property('version_id').to.be.an('string');
expect(res).to.have.property('quiz_version_id').to.be.an('string');
expect(res).to.have.property('quiz_session_id').to.be.an('string');
expect(res).to.have.property('quiz_id').to.be.an('string');
expect(fetchSpy).to.have.been.called;
expect(requestedUrlParams).to.have.property('filters');
expect(requestedUrlParams.filters).to.have.property('Color').to.equal(Object.values(filters)[0][0]);
});
});

it('Should be rejected if no quizId is provided', () => {
it('Should be rejected if an invalid quizVersionId is provided', () => {
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return expect(quizzes.getQuizResults(null, { answers: validAnswers })).to.eventually.be.rejected;
return expect(quizzes.getQuizResults(validQuizId, { answers: validAnswers, quizVersionId: 'foo' })).to.eventually.be.rejected;
});

it('Should be rejected if an invalid quizId is provided', () => {
it('Should be rejected if no quizId is provided', () => {
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return expect(quizzes.getQuizResults('invalidQuizId', { answers: validAnswers })).to.eventually.be.rejected;
return expect(quizzes.getQuizResults(null, { answers: validAnswers })).to.eventually.be.rejected;
});

it('Should be rejected if an invalid versionId is provided', () => {
it('Should be rejected if an invalid quizId is provided', () => {
const { quizzes } = new ConstructorIO({
apiKey: quizApiKey,
fetch: fetchSpy,
});

return expect(quizzes.getQuizResults(validQuizId, { answers: validAnswers, versionId: 'foo' })).to.eventually.be.rejected;
return expect(quizzes.getQuizResults('invalidQuizId', { answers: validAnswers })).to.eventually.be.rejected;
});

it('Should be rejected if an invalid apiKey is provided', () => {
Expand Down
29 changes: 19 additions & 10 deletions src/modules/quizzes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ function createQuizUrl(quizId, parameters, options, path) {
}

if (parameters) {
const { section, answers, versionId, page, resultsPerPage, filters } = parameters;
const { section, answers, quizSessionId, quizVersionId, page, resultsPerPage, filters } = parameters;

// Pull section from parameters
if (section) {
queryParams.section = section;
}

// Pull version_id from parameters
if (versionId) {
queryParams.version_id = versionId;
// Pull quiz_version_id from parameters
if (quizVersionId) {
queryParams.quiz_version_id = quizVersionId;
}

// Pull quiz_session_id from parameters
if (quizSessionId) {
queryParams.quiz_session_id = quizSessionId;
}

// Pull a (answers) from parameters and transform
Expand Down Expand Up @@ -102,7 +107,8 @@ class Quizzes {
* @param {string} [parameters] - Additional parameters to refine result set
* @param {string} [parameters.section] - Product catalog section
* @param {array} [parameters.answers] - An array of answers in the format [[1,2],[1]]
* @param {string} [parameters.versionId] - Version identifier for the quiz
* @param {string} [parameters.quizVersionId] - Version identifier for the quiz. Version ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-versioning
* @param {string} [parameters.quizSessionId] - Session identifier for the quiz. Session ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-sessions
* @param {object} [networkParameters] - Parameters relevant to the network request
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
* @returns {Promise}
Expand All @@ -111,7 +117,8 @@ class Quizzes {
* constructorio.quizzes.getQuizNextQuestion('quizId', {
* answers: [[1,2],[1]],
* section: '123',
* versionId: '123'
* quizVersionId: '123',
* quizSessionId: '1234',
* });
*/
getQuizNextQuestion(id, parameters, networkParameters = {}) {
Expand All @@ -138,7 +145,7 @@ class Quizzes {
return helpers.throwHttpErrorFromResponse(new Error(), response);
})
.then((json) => {
if (json.version_id) {
if (json.quiz_version_id) {
this.eventDispatcher.queue('quizzes.getQuizNextQuestion.completed', json);

return json;
Expand All @@ -157,7 +164,8 @@ class Quizzes {
* @param {string} parameters - Additional parameters to refine result set
* @param {array} parameters.answers - An array of answers in the format [[1,2],[1]]
* @param {string} [parameters.section] - Product catalog section
* @param {string} [parameters.versionId] - Specific version identifier for the quiz
* @param {string} [parameters.quizVersionId] - Version identifier for the quiz. Version ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-versioning
* @param {string} [parameters.quizSessionId] - Session identifier for the quiz. Session ID will be returned with the first request and it should be passed with subsequent requests. More information can be found: https://docs.constructor.io/rest_api/quiz/using_quizzes/#quiz-sessions
* @param {number} [parameters.page] - The page number of the results
* @param {number} [parameters.resultsPerPage] - The number of results per page to return
* @param {object} [parameters.filters] - Key / value mapping (dictionary) of filters used to refine results
Expand All @@ -169,7 +177,8 @@ class Quizzes {
* constructorio.quizzes.getQuizResults('quizId', {
* answers: [[1,2],[1]],
* section: '123',
* versionId: '123'
* quizVersionId: '123',
* quizSessionId: '234'
* });
*/
getQuizResults(id, parameters, networkParameters = {}) {
Expand All @@ -196,7 +205,7 @@ class Quizzes {
return helpers.throwHttpErrorFromResponse(new Error(), response);
})
.then((json) => {
if (json.version_id) {
if (json.quiz_version_id) {
this.eventDispatcher.queue('quizzes.getQuizResults.completed', json);

return json;
Expand Down
7 changes: 5 additions & 2 deletions src/types/quizzes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default Quizzes;
export interface QuizzesParameters {
section?: string;
answers?: any[];
versionId?: string;
quizVersionId?: string;
quizSessionId?: string;
}

export interface QuizzesResultsParameters extends QuizzesParameters {
Expand Down Expand Up @@ -50,6 +51,8 @@ export interface NextQuestionResponse extends Record<string, any> {
next_question: Question;
is_last_question?: boolean;
version_id?: string;
quiz_id?: string;
quiz_session_id?: string;
}

export interface QuizResultsResponse extends Record<string, any> {
Expand Down Expand Up @@ -77,7 +80,7 @@ export interface QuizResultsResponse extends Record<string, any> {
features: Partial<Feature>[];
};
result_id?: string;
version_id: string;
quiz_version_id: string;
quiz_session_id: string;
quiz_id: string;
}
Expand Down