diff --git a/src/adapters/Survey/index.test.ts b/src/adapters/Survey/index.test.ts index ab1f679..44930be 100644 --- a/src/adapters/Survey/index.test.ts +++ b/src/adapters/Survey/index.test.ts @@ -1,18 +1,12 @@ -import { getAuth } from 'adapters/BaseAuth'; +import { getAuth, postAuth } from 'adapters/BaseAuth'; +import { surveySubmitRequestFabricator } from 'tests/fabricator'; +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()); - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - describe('getSurveys', () => { it('calls the get method from the base adapter', () => { const expectedPath = 'surveys?page[number]=1&page[size]=10'; @@ -33,4 +27,15 @@ describe('SurveyAdapter', () => { expect(getAuth).toHaveBeenCalledWith(expectedPath); }); }); + + describe('submitSurvey', () => { + it('calls the post method from the base adapter', () => { + const payload: SurveySubmitRequest = surveySubmitRequestFabricator(); + 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..791e9e5 100644 --- a/src/adapters/Survey/index.ts +++ b/src/adapters/Survey/index.ts @@ -1,5 +1,10 @@ -import { getAuth } from 'adapters/BaseAuth'; +import { getAuth, postAuth } from 'adapters/BaseAuth'; +import { JSONObject } from 'helpers/json'; +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 as unknown as JSONObject); diff --git a/src/helpers/json.ts b/src/helpers/json.ts index 31f2169..152670d 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/tests/fabricator.ts b/src/tests/fabricator.ts index 58cc8c3..f7bbe0d 100644 --- a/src/tests/fabricator.ts +++ b/src/tests/fabricator.ts @@ -12,6 +12,22 @@ export const testTypeFabricator = Fabricator({ }, }); +// Requests +const answerRequestFabricator = Fabricator({ + id: () => sequence('answerId').toString(), + answer: () => faker.string.sample(), +}); + +const questionRequestFabricator = Fabricator({ + id: () => sequence('questionId').toString(), + answers: () => answerRequestFabricator.times(2), +}); + +export const surveySubmitRequestFabricator = Fabricator({ + surveyId: () => sequence('surveyId').toString(), + questions: () => questionRequestFabricator.times(10), +}); + // Responses const questionId = faker.string.uuid(); diff --git a/src/types/request/surveySubmitRequest.ts b/src/types/request/surveySubmitRequest.ts new file mode 100644 index 0000000..c07795a --- /dev/null +++ b/src/types/request/surveySubmitRequest.ts @@ -0,0 +1,14 @@ +export interface SurveySubmitRequest { + surveyId: string; + questions: QuestionRequest[]; +} + +interface QuestionRequest { + id: string; + answers: AnswerRequest[]; +} + +interface AnswerRequest { + id: string; + answer?: string; +}