Skip to content

Commit

Permalink
[#31] Implement the backend POST submit survey
Browse files Browse the repository at this point in the history
  • Loading branch information
manh-t committed Jul 21, 2023
1 parent 7ff8cb6 commit c832741
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
38 changes: 36 additions & 2 deletions src/adapters/Survey/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { getAuth } from 'adapters/BaseAuth';
import { getAuth, postAuth } from 'adapters/BaseAuth';
import { SurveySubmitRequest } from 'types/request/surveySubmitRequest';

import { getSurvey, getSurveys } from '.';
import { getSurvey, getSurveys, submitSurvey } from '.';

jest.mock('adapters/BaseAuth');

describe('SurveyAdapter', () => {
beforeEach(() => {
(getAuth as jest.Mock).mockImplementation(() => jest.fn());
(postAuth as jest.Mock).mockImplementation(() => jest.fn());
});

afterEach(() => {
Expand All @@ -33,4 +35,36 @@ describe('SurveyAdapter', () => {
expect(getAuth).toHaveBeenCalledWith(expectedPath);
});
});

describe('submitSurvey', () => {
it('calls the post method from the base adapter', () => {
const payload: SurveySubmitRequest = {
surveyId: 'd5de6a8f8f5f1cfe51bc',
questions: [
{
id: '940d229e4cd87cd1e202',
answers: [
{
id: '037574cb93d16800eecd',
},
],
},
{
id: 'c3a9b8ce5c2356010703',
answers: [
{
id: '2a49e148c5b170aca804',
answer: 'My answer',
},
],
},
],
};
const expectedPath = `responses`;

submitSurvey(payload);

expect(postAuth).toHaveBeenCalledWith(expectedPath, payload);
});
});
});
5 changes: 4 additions & 1 deletion src/adapters/Survey/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { getAuth } from 'adapters/BaseAuth';
import { getAuth, postAuth } from 'adapters/BaseAuth';
import { SurveySubmitRequest } from 'types/request/surveySubmitRequest';

export const getSurveys = () => getAuth('surveys?page[number]=1&page[size]=10');

export const getSurvey = (surveyId: string) => getAuth(`surveys/${surveyId}`);

export const submitSurvey = (surveySubmitRequest: SurveySubmitRequest) => postAuth('responses', surveySubmitRequest);
4 changes: 2 additions & 2 deletions src/helpers/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export type JSONValue = string | number | boolean | JSONObject | JSONArray;
export type JSONArray = Array<JSONValue>;

export interface JSONObject {
[key: string]: JSONValue;
[key: string]: JSONValue | undefined;
}

const convertKeys = (
json: JSONValue | JSONObject | JSONArray,
convertKeyCallback: (key: string) => string,
convertJsonCallback: (json: JSONValue | JSONObject | JSONArray) => JSONValue | JSONObject | JSONArray
convertJsonCallback: (json: JSONValue | JSONObject | JSONArray | undefined) => JSONValue | JSONObject | JSONArray
) => {
if (Array.isArray(json)) {
return json.map((jsonObject) => convertJsonCallback(jsonObject as JSONValue));
Expand Down
16 changes: 16 additions & 0 deletions src/types/request/surveySubmitRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JSONObject } from 'helpers/json';

export interface SurveySubmitRequest extends JSONObject {
surveyId: string;
questions: QuestionRequest[];
}

interface QuestionRequest extends JSONObject {
id: string;
answers: AnswerRequest[];
}

interface AnswerRequest extends JSONObject {
id: string;
answer?: string;
}

0 comments on commit c832741

Please sign in to comment.