diff --git a/src/adapters/Survey/index.test.ts b/src/adapters/Survey/index.test.ts index ab1f679..02f16d9 100644 --- a/src/adapters/Survey/index.test.ts +++ b/src/adapters/Survey/index.test.ts @@ -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(() => { @@ -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); + }); + }); }); diff --git a/src/adapters/Survey/index.ts b/src/adapters/Survey/index.ts index a6a088c..4a0636d 100644 --- a/src/adapters/Survey/index.ts +++ b/src/adapters/Survey/index.ts @@ -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); diff --git a/src/helpers/json.ts b/src/helpers/json.ts index f24aeec..ca62c2e 100644 --- a/src/helpers/json.ts +++ b/src/helpers/json.ts @@ -6,13 +6,13 @@ export type JSONValue = string | number | boolean | JSONObject | JSONArray; export type JSONArray = Array; 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)); diff --git a/src/types/request/surveySubmitRequest.ts b/src/types/request/surveySubmitRequest.ts new file mode 100644 index 0000000..8157dfd --- /dev/null +++ b/src/types/request/surveySubmitRequest.ts @@ -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; +}