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 Sep 7, 2023
1 parent 1fffacf commit da6188c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/adapters/Survey/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
});
});
});
7 changes: 6 additions & 1 deletion src/adapters/Survey/index.ts
Original file line number Diff line number Diff line change
@@ -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);
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/tests/fabricator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 14 additions & 0 deletions src/types/request/surveySubmitRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface SurveySubmitRequest {
surveyId: string;
questions: QuestionRequest[];
}

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

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

0 comments on commit da6188c

Please sign in to comment.