From 9532f2721c52af8a25e8bc7ceb819b11bca6f03a Mon Sep 17 00:00:00 2001 From: Albert Alises Date: Fri, 23 Oct 2020 16:21:38 +0200 Subject: [PATCH] refactor(datasources): unify all metro data sources into one --- ...oLinesDataSource.ts => MetroDataSource.ts} | 117 ++++++++++-- src/datasources/MetroStationsDataSource.ts | 107 ----------- .../__tests__/MetroDataSource.test.ts | 178 ++++++++++++++++++ .../__tests__/MetroLinesDataSource.test.ts | 92 --------- .../__tests__/MetroStationsDataSource.test.ts | 98 ---------- src/graphql.ts | 6 +- src/queries/MetroLineQuery.ts | 2 +- src/queries/MetroLinesQuery.ts | 2 +- src/queries/MetroStationQuery.ts | 2 +- src/queries/MetroStationsQuery.ts | 2 +- src/queries/__tests__/MetroLineQuery.test.ts | 6 +- src/queries/__tests__/MetroLinesQuery.test.ts | 4 +- .../__tests__/MetroStationQuery.test.ts | 4 +- .../__tests__/MetroStationsQuery.test.ts | 6 +- src/utils/createTestServer.ts | 14 +- 15 files changed, 302 insertions(+), 338 deletions(-) rename src/datasources/{MetroLinesDataSource.ts => MetroDataSource.ts} (61%) delete mode 100644 src/datasources/MetroStationsDataSource.ts create mode 100644 src/datasources/__tests__/MetroDataSource.test.ts delete mode 100644 src/datasources/__tests__/MetroLinesDataSource.test.ts delete mode 100644 src/datasources/__tests__/MetroStationsDataSource.test.ts diff --git a/src/datasources/MetroLinesDataSource.ts b/src/datasources/MetroDataSource.ts similarity index 61% rename from src/datasources/MetroLinesDataSource.ts rename to src/datasources/MetroDataSource.ts index 731c98a..c550316 100644 --- a/src/datasources/MetroLinesDataSource.ts +++ b/src/datasources/MetroDataSource.ts @@ -1,12 +1,10 @@ -import { TMB_API_BASE_URL } from "../config"; -import { ApolloError, ValidationError } from "apollo-server-lambda"; -import { +import TmbApiDataSource from "./TmbApiDataSource"; +import type { FindByInput, - MetroLine as MetroLineType, MetroStation as MetroStationType, + MetroLine as MetroLineType, } from "../../types"; -import MetroStationDataSource from "./MetroStationsDataSource"; -import TmbApiDataSource from "./TmbApiDataSource"; +import { ApolloError, ValidationError } from "apollo-server-lambda"; export interface MetroLineAPIType { type: string; @@ -55,10 +53,104 @@ interface MetroLinesAPIType { }; } -export default class MetroLinesDataSource extends TmbApiDataSource { - constructor() { - super(); - this.baseURL = TMB_API_BASE_URL; +export interface MetroStationAPIType { + type: string; + id: string; + geometry: { + type: string; + coordinates: number[]; + }; + geometry_name: string; + properties: { + CODI_GRUP_STACIO: number; + NOM_STACIO: string; + PICTO: string; + DATA: string; + }; +} + +interface MetroStationsAPIType { + type: "FeatureCollection"; + features: ReadonlyArray; + totalFeatures: number; + numberMatched: number; + numberReturned: number; + timeStamp: string; + crs: { + type: "name"; + properties: { + name: string; + }; + }; +} + +export default class MetroDataSource extends TmbApiDataSource { + //Transforms e.g. L1L2 into [L1, L2] + parseLines(lines: string): string[] { + return lines.replace(/L/g, ",L").split(",").filter(Boolean); + } + + metroStationReducer(data: MetroStationAPIType): MetroStationType { + return { + id: data.properties["CODI_GRUP_ESTACIO"], + name: data.properties["NOM_ESTACIO"], + location: { + longitude: data.geometry.coordinates[0], + latitude: data.geometry.coordinates[1], + }, + lines: this.parseLines(data.properties["PICTO"]), + }; + } + + async getStation({ + id, + name, + }: FindByInput): Promise { + if (!id && !name) { + return new ValidationError( + "You need to provide either a valid ID or a valid name" + ); + } + + const path = ["estacions", id].filter(Boolean).join("/"); + const nameFilterParameter = name ? { filter: `NOM_ESTACIO='${name}'` } : {}; + + const response: MetroStationsAPIType | null = await this.get( + path, + nameFilterParameter + ); + + if (Array.isArray(response?.features) && response?.features.length === 0) { + return new ApolloError( + `No stations were found with these parameters: ${JSON.stringify({ + id, + name, + })}` + ); + } + + const station: MetroStationAPIType | null = response?.features?.[0] ?? null; + + if (station == null) { + return new ApolloError("The station object returned did not exist"); + } + + return this.metroStationReducer(station); + } + + async getAllStations(): Promise<{ + numberOfStations: number | null; + stations: MetroStationType[]; + }> { + const response: MetroStationsAPIType | null = await this.get("estacions"); + + return { + numberOfStations: response?.numberReturned ?? null, + stations: + response?.features?.map((station: MetroStationAPIType) => + this.metroStationReducer(station) + ) ?? [], + }; } metroLineReducer({ properties }: MetroLineAPIType): MetroLineType { @@ -82,9 +174,8 @@ export default class MetroLinesDataSource extends TmbApiDataSource { const response = await this.get(path, nameFilterParameter); const stations = - response?.features?.map((station) => - new MetroStationDataSource().metroStationReducer(station) - ) ?? []; + response?.features?.map((station) => this.metroStationReducer(station)) ?? + []; return stations; } diff --git a/src/datasources/MetroStationsDataSource.ts b/src/datasources/MetroStationsDataSource.ts deleted file mode 100644 index 4935903..0000000 --- a/src/datasources/MetroStationsDataSource.ts +++ /dev/null @@ -1,107 +0,0 @@ -import TmbApiDataSource from "./TmbApiDataSource"; -import type { - FindByInput, - MetroStation as MetroStationType, -} from "../../types"; -import { ApolloError, ValidationError } from "apollo-server-lambda"; - -export interface MetroStationAPIType { - type: string; - id: string; - geometry: { - type: string; - coordinates: number[]; - }; - geometry_name: string; - properties: { - CODI_GRUP_STACIO: number; - NOM_STACIO: string; - PICTO: string; - DATA: string; - }; -} - -interface MetroStationsAPIType { - type: "FeatureCollection"; - features: ReadonlyArray; - totalFeatures: number; - numberMatched: number; - numberReturned: number; - timeStamp: string; - crs: { - type: "name"; - properties: { - name: string; - }; - }; -} - -export default class MetroStationsDataSource extends TmbApiDataSource { - //Transforms e.g. L1L2 into [L1, L2] - parseLines(lines: string): string[] { - return lines.replace(/L/g, ",L").split(",").filter(Boolean); - } - - metroStationReducer(data: MetroStationAPIType): MetroStationType { - return { - id: data.properties["CODI_GRUP_ESTACIO"], - name: data.properties["NOM_ESTACIO"], - location: { - longitude: data.geometry.coordinates[0], - latitude: data.geometry.coordinates[1], - }, - lines: this.parseLines(data.properties["PICTO"]), - }; - } - - async getStation({ - id, - name, - }: FindByInput): Promise { - if (!id && !name) { - return new ValidationError( - "You need to provide either a valid ID or a valid name" - ); - } - - const path = ["estacions", id].filter(Boolean).join("/"); - const nameFilterParameter = name ? { filter: `NOM_ESTACIO='${name}'` } : {}; - - const response: MetroStationsAPIType | null = await this.get( - path, - nameFilterParameter - ); - - if (Array.isArray(response?.features) && response?.features.length === 0) { - return new ApolloError( - `No stations were found with these parameters: ${JSON.stringify({ - id, - name, - })}` - ); - } - - const station: MetroStationAPIType | null = response?.features?.[0] ?? null; - - if (station == null) { - return new ApolloError("The station object returned did not exist"); - } - - return this.metroStationReducer(station); - } - - async getAllStations(): Promise<{ - numberOfStations: number | null; - stations: MetroStationType[]; - }> { - const response: MetroStationsAPIType | null = await this.get("estacions"); - - return { - numberOfStations: response?.numberReturned ?? null, - stations: - response?.features?.map((station: MetroStationAPIType) => - this.metroStationReducer(station) - ) ?? [], - }; - } -} diff --git a/src/datasources/__tests__/MetroDataSource.test.ts b/src/datasources/__tests__/MetroDataSource.test.ts new file mode 100644 index 0000000..10091ef --- /dev/null +++ b/src/datasources/__tests__/MetroDataSource.test.ts @@ -0,0 +1,178 @@ +import DataSource from "../MetroDataSource"; +import { ApolloError, ValidationError } from "apollo-server-lambda"; +import { + mockMetroStationsAPIResponse, + mockMetroStationsResponse, +} from "../__fixtures__/MetroStationsFixtures"; +import { + mockMetroLinesAPIResponse, + mockMetroLinesResponse, +} from "../__fixtures__/MetroLinesFixtures"; + +const MetroDataSource = new DataSource(); + +describe("MetroDataSource", () => { + const mockGet = jest.fn(); + + MetroDataSource.get = mockGet; + MetroDataSource.getLineStations = jest + .fn() + .mockReturnValue(mockMetroLinesResponse.lines[0].stations); + + describe("[getAllLines]", () => { + it("Correctly looks up the lines from the API", async () => { + mockGet.mockReturnValueOnce(mockMetroLinesAPIResponse); + + const res = await MetroDataSource.getAllLines(); + expect(res).toEqual(mockMetroLinesResponse); + + expect(mockGet.mock.calls[0][0]).toBe("linies/metro"); + }); + }); + describe("[getLine]", () => { + it("Throws a Validation Error if a falsy ID and name are passed as parameter", async () => { + const res = await MetroDataSource.getLine({ + id: null, + name: null, + }); + + expect(res).toBeInstanceOf(ValidationError); + }); + + it("Throws a Not Found Error if the response does not contain features", async () => { + mockGet.mockReturnValueOnce({ features: [] }); + const res = await MetroDataSource.getLine({ id: 32 }); + + expect(res).toBeInstanceOf(ApolloError); + }); + + it("Throws an Error if the features are null or undefined", async () => { + mockGet.mockReturnValueOnce({ features: null }); + const res = await MetroDataSource.getLine({ + name: "L4", + }); + + expect(res).toBeInstanceOf(ApolloError); + }); + + it("Correctly gets a line by ID", async () => { + mockGet.mockReturnValueOnce({ + features: [mockMetroLinesAPIResponse.features[0]], + }); + const res = await MetroDataSource.getLine({ id: 32 }); + + expect(res).toEqual(mockMetroLinesResponse.lines[0]); + expect(mockGet.mock.calls[0][0]).toBe("linies/metro/32"); + }); + + it("Correctly gets a line by Name", async () => { + mockGet.mockReturnValueOnce({ + features: [mockMetroLinesAPIResponse.features[0]], + }); + const res = await MetroDataSource.getLine({ + name: "L4", + }); + + expect(res).toEqual(mockMetroLinesResponse.lines[0]); + expect(mockGet).toBeCalledWith("linies/metro", { + filter: "NOM_LINIA='L4'", + }); + }); + }); + + it("[metroLineReducer]: Correctly parses a metro line API data to the schema format", () => { + const lineResponse = mockMetroLinesResponse.lines[0]; + expect( + MetroDataSource.metroLineReducer( + mockMetroLinesAPIResponse.features[0] as any + ) + ).toEqual({ + ...lineResponse, + endingStation: lineResponse.endingStation.name, + originStation: lineResponse.originStation.name, + stations: null, + }); + }); + + describe("[getAllStations]", () => { + it("Correctly looks up the stations from the API", async () => { + mockGet.mockReturnValueOnce(mockMetroStationsAPIResponse); + + const res = await MetroDataSource.getAllStations(); + expect(res).toEqual(mockMetroStationsResponse); + + expect(mockGet.mock.calls[0][0]).toBe("estacions"); + }); + }); + + describe("[getStation]", () => { + it("Throws a Validation Error if a falsy ID and name are passed as parameter", async () => { + const res = await MetroDataSource.getStation({ + id: null, + name: null, + }); + + expect(res).toBeInstanceOf(ValidationError); + }); + + it("Throws a Not Found Error if the response does not contain features", async () => { + mockGet.mockReturnValueOnce({ features: [] }); + const res = await MetroDataSource.getStation({ id: 32 }); + + expect(res).toBeInstanceOf(ApolloError); + }); + + it("Throws an Error if the features are null or undefined", async () => { + mockGet.mockReturnValueOnce({ features: null }); + const res = await MetroDataSource.getStation({ + name: "Urwhatawave", + }); + + expect(res).toBeInstanceOf(ApolloError); + }); + + it("Correctly gets a station by ID", async () => { + mockGet.mockReturnValueOnce({ + features: [mockMetroStationsAPIResponse.features[0]], + }); + const res = await MetroDataSource.getStation({ id: 32 }); + + expect(res).toEqual(mockMetroStationsResponse.stations[0]); + expect(mockGet.mock.calls[0][0]).toBe("estacions/32"); + }); + + it("Correctly gets a station by Name", async () => { + mockGet.mockReturnValueOnce({ + features: [mockMetroStationsAPIResponse.features[0]], + }); + const res = await MetroDataSource.getStation({ + name: "Urwhatawave", + }); + + expect(res).toEqual(mockMetroStationsResponse.stations[0]); + expect(mockGet).toBeCalledWith("estacions", { + filter: "NOM_ESTACIO='Urwhatawave'", + }); + }); + }); + + it("[metroStationReducer]: Correctly parses an station API data to the schema format", () => { + expect( + MetroDataSource.metroStationReducer( + mockMetroStationsAPIResponse.features[0] as any + ) + ).toEqual(mockMetroStationsResponse.stations[0]); + }); + + test.each([ + ["L1", ["L1"]], + ["", []], + ["L1L2", ["L1", "L2"]], + ["L4L5L10N", ["L4", "L5", "L10N"]], + ])( + "[parseLines]: Parses the line string %p to be %p", + (lineString, parsedLineString) => { + expect(MetroDataSource.parseLines(lineString)).toEqual(parsedLineString); + } + ); +}); diff --git a/src/datasources/__tests__/MetroLinesDataSource.test.ts b/src/datasources/__tests__/MetroLinesDataSource.test.ts deleted file mode 100644 index a914c8e..0000000 --- a/src/datasources/__tests__/MetroLinesDataSource.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import DataSource from "../MetroLinesDataSource"; -import { ApolloError, ValidationError } from "apollo-server-lambda"; -import { - mockMetroLinesAPIResponse, - mockMetroLinesResponse, -} from "../__fixtures__/MetroLinesFixtures"; - -const MetroLinesDataSource = new DataSource(); - -describe("MetroLinesDataSource", () => { - const mockGet = jest.fn(); - - MetroLinesDataSource.get = mockGet; - MetroLinesDataSource.getLineStations = jest - .fn() - .mockReturnValue(mockMetroLinesResponse.lines[0].stations); - - describe("[getAllLines]", () => { - it("Correctly looks up the lines from the API", async () => { - mockGet.mockReturnValueOnce(mockMetroLinesAPIResponse); - - const res = await MetroLinesDataSource.getAllLines(); - expect(res).toEqual(mockMetroLinesResponse); - - expect(mockGet.mock.calls[0][0]).toBe("linies/metro"); - }); - }); - describe("[getLine]", () => { - it("Throws a Validation Error if a falsy ID and name are passed as parameter", async () => { - const res = await MetroLinesDataSource.getLine({ - id: null, - name: null, - }); - - expect(res).toBeInstanceOf(ValidationError); - }); - - it("Throws a Not Found Error if the response does not contain features", async () => { - mockGet.mockReturnValueOnce({ features: [] }); - const res = await MetroLinesDataSource.getLine({ id: 32 }); - - expect(res).toBeInstanceOf(ApolloError); - }); - - it("Throws an Error if the features are null or undefined", async () => { - mockGet.mockReturnValueOnce({ features: null }); - const res = await MetroLinesDataSource.getLine({ - name: "L4", - }); - - expect(res).toBeInstanceOf(ApolloError); - }); - - it("Correctly gets a line by ID", async () => { - mockGet.mockReturnValueOnce({ - features: [mockMetroLinesAPIResponse.features[0]], - }); - const res = await MetroLinesDataSource.getLine({ id: 32 }); - - expect(res).toEqual(mockMetroLinesResponse.lines[0]); - expect(mockGet.mock.calls[0][0]).toBe("linies/metro/32"); - }); - - it("Correctly gets a line by Name", async () => { - mockGet.mockReturnValueOnce({ - features: [mockMetroLinesAPIResponse.features[0]], - }); - const res = await MetroLinesDataSource.getLine({ - name: "L4", - }); - - expect(res).toEqual(mockMetroLinesResponse.lines[0]); - expect(mockGet).toBeCalledWith("linies/metro", { - filter: "NOM_LINIA='L4'", - }); - }); - }); - - it("[metroLineReducer]: Correctly parses a metro line API data to the schema format", () => { - const lineResponse = mockMetroLinesResponse.lines[0]; - expect( - MetroLinesDataSource.metroLineReducer( - mockMetroLinesAPIResponse.features[0] as any - ) - ).toEqual({ - ...lineResponse, - endingStation: lineResponse.endingStation.name, - originStation: lineResponse.originStation.name, - stations: null, - }); - }); -}); diff --git a/src/datasources/__tests__/MetroStationsDataSource.test.ts b/src/datasources/__tests__/MetroStationsDataSource.test.ts deleted file mode 100644 index e19a4b4..0000000 --- a/src/datasources/__tests__/MetroStationsDataSource.test.ts +++ /dev/null @@ -1,98 +0,0 @@ -import DataSource from "../MetroStationsDataSource"; -import { ApolloError, ValidationError } from "apollo-server-lambda"; -import { - mockMetroStationsAPIResponse, - mockMetroStationsResponse, -} from "../__fixtures__/MetroStationsFixtures"; - -const MetroStationsDataSource = new DataSource(); - -describe("MetroStationsMetroStationsDataSource", () => { - const mockGet = jest.fn(); - - MetroStationsDataSource.get = mockGet; - - describe("[getAllStations]", () => { - it("Correctly looks up the stations from the API", async () => { - mockGet.mockReturnValueOnce(mockMetroStationsAPIResponse); - - const res = await MetroStationsDataSource.getAllStations(); - expect(res).toEqual(mockMetroStationsResponse); - - expect(mockGet.mock.calls[0][0]).toBe("estacions"); - }); - }); - - describe("[getStation]", () => { - it("Throws a Validation Error if a falsy ID and name are passed as parameter", async () => { - const res = await MetroStationsDataSource.getStation({ - id: null, - name: null, - }); - - expect(res).toBeInstanceOf(ValidationError); - }); - - it("Throws a Not Found Error if the response does not contain features", async () => { - mockGet.mockReturnValueOnce({ features: [] }); - const res = await MetroStationsDataSource.getStation({ id: 32 }); - - expect(res).toBeInstanceOf(ApolloError); - }); - - it("Throws an Error if the features are null or undefined", async () => { - mockGet.mockReturnValueOnce({ features: null }); - const res = await MetroStationsDataSource.getStation({ - name: "Urwhatawave", - }); - - expect(res).toBeInstanceOf(ApolloError); - }); - - it("Correctly gets a station by ID", async () => { - mockGet.mockReturnValueOnce({ - features: [mockMetroStationsAPIResponse.features[0]], - }); - const res = await MetroStationsDataSource.getStation({ id: 32 }); - - expect(res).toEqual(mockMetroStationsResponse.stations[0]); - expect(mockGet.mock.calls[0][0]).toBe("estacions/32"); - }); - - it("Correctly gets a station by Name", async () => { - mockGet.mockReturnValueOnce({ - features: [mockMetroStationsAPIResponse.features[0]], - }); - const res = await MetroStationsDataSource.getStation({ - name: "Urwhatawave", - }); - - expect(res).toEqual(mockMetroStationsResponse.stations[0]); - expect(mockGet).toBeCalledWith("estacions", { - filter: "NOM_ESTACIO='Urwhatawave'", - }); - }); - }); - - it("[metroStationReducer]: Correctly parses an station API data to the schema format", () => { - expect( - MetroStationsDataSource.metroStationReducer( - mockMetroStationsAPIResponse.features[0] as any - ) - ).toEqual(mockMetroStationsResponse.stations[0]); - }); - - test.each([ - ["L1", ["L1"]], - ["", []], - ["L1L2", ["L1", "L2"]], - ["L4L5L10N", ["L4", "L5", "L10N"]], - ])( - "[parseLines]: Parses the line string %p to be %p", - (lineString, parsedLineString) => { - expect(MetroStationsDataSource.parseLines(lineString)).toEqual( - parsedLineString - ); - } - ); -}); diff --git a/src/graphql.ts b/src/graphql.ts index 00e110f..34b5ab3 100644 --- a/src/graphql.ts +++ b/src/graphql.ts @@ -1,7 +1,6 @@ import { ApolloServer } from "apollo-server-lambda"; import schema from "./schema"; -import MetroStationsDataSource from "./datasources/MetroStationsDataSource"; -import MetroLinesDataSource from "./datasources/MetroLinesDataSource"; +import MetroDataSource from "./datasources/MetroDataSource"; import formatError from "./utils/formatError"; const server: ApolloServer = new ApolloServer({ @@ -14,8 +13,7 @@ const server: ApolloServer = new ApolloServer({ formatError, introspection: true, dataSources: () => ({ - metroStations: new MetroStationsDataSource(), - metroLines: new MetroLinesDataSource(), + metro: new MetroDataSource(), }), }); diff --git a/src/queries/MetroLineQuery.ts b/src/queries/MetroLineQuery.ts index 55e646a..f218419 100644 --- a/src/queries/MetroLineQuery.ts +++ b/src/queries/MetroLineQuery.ts @@ -19,6 +19,6 @@ export default { ); } - return await dataSources.metroLines.getLine(findBy); + return await dataSources.metro.getLine(findBy); }, }; diff --git a/src/queries/MetroLinesQuery.ts b/src/queries/MetroLinesQuery.ts index 461a6b6..809b565 100644 --- a/src/queries/MetroLinesQuery.ts +++ b/src/queries/MetroLinesQuery.ts @@ -35,7 +35,7 @@ export default { args: ConnectionArguments, { dataSources } ): Promise => { - const { numberOfLines, lines } = await dataSources.metroLines.getAllLines(); + const { numberOfLines, lines } = await dataSources.metro.getAllLines(); return { numberOfLines, diff --git a/src/queries/MetroStationQuery.ts b/src/queries/MetroStationQuery.ts index e29da96..942ba3a 100644 --- a/src/queries/MetroStationQuery.ts +++ b/src/queries/MetroStationQuery.ts @@ -23,6 +23,6 @@ export default { ); } - return await dataSources.metroStations.getStation(findBy); + return await dataSources.metro.getStation(findBy); }, }; diff --git a/src/queries/MetroStationsQuery.ts b/src/queries/MetroStationsQuery.ts index 89fd801..c0a0257 100644 --- a/src/queries/MetroStationsQuery.ts +++ b/src/queries/MetroStationsQuery.ts @@ -36,7 +36,7 @@ export default { const { numberOfStations, stations, - } = await dataSources.metroStations.getAllStations(); + } = await dataSources.metro.getAllStations(); return { numberOfStations, diff --git a/src/queries/__tests__/MetroLineQuery.test.ts b/src/queries/__tests__/MetroLineQuery.test.ts index e02a0a4..88a6a47 100644 --- a/src/queries/__tests__/MetroLineQuery.test.ts +++ b/src/queries/__tests__/MetroLineQuery.test.ts @@ -17,14 +17,14 @@ const GET_METRO_LINE = gql` `; describe("MetroStation Query", () => { - const { server, metroLines } = createTestServer(); + const { server, metro } = createTestServer(); const { query } = createTestClient(server); - metroLines.getLineStations = jest + metro.getLineStations = jest .fn() .mockReturnValue(mockMetroLinesResponse.lines[0].stations); - metroLines.get = jest.fn().mockReturnValue({ + metro.get = jest.fn().mockReturnValue({ ...mockMetroLinesAPIResponse, features: [mockMetroLinesAPIResponse.features[0]], }); diff --git a/src/queries/__tests__/MetroLinesQuery.test.ts b/src/queries/__tests__/MetroLinesQuery.test.ts index 2980ec6..08239eb 100644 --- a/src/queries/__tests__/MetroLinesQuery.test.ts +++ b/src/queries/__tests__/MetroLinesQuery.test.ts @@ -21,8 +21,8 @@ const GET_METRO_LINES = gql` describe("metroLines Query", () => { it("Fetches list of metro lines", async () => { - const { server, metroLines } = createTestServer(); - metroLines.get = jest.fn().mockReturnValueOnce(mockMetroLinesAPIResponse); + const { server, metro } = createTestServer(); + metro.get = jest.fn().mockReturnValueOnce(mockMetroLinesAPIResponse); const { query } = createTestClient(server); const res = await query({ diff --git a/src/queries/__tests__/MetroStationQuery.test.ts b/src/queries/__tests__/MetroStationQuery.test.ts index 545d25f..b5acb92 100644 --- a/src/queries/__tests__/MetroStationQuery.test.ts +++ b/src/queries/__tests__/MetroStationQuery.test.ts @@ -14,10 +14,10 @@ const GET_METRO_STATION = gql` `; describe("MetroStation Query", () => { - const { server, metroStations } = createTestServer(); + const { server, metro } = createTestServer(); const { query } = createTestClient(server); - metroStations.get = jest.fn().mockReturnValue({ + metro.get = jest.fn().mockReturnValue({ ...mockMetroStationsAPIResponse, features: [mockMetroStationsAPIResponse.features[0]], }); diff --git a/src/queries/__tests__/MetroStationsQuery.test.ts b/src/queries/__tests__/MetroStationsQuery.test.ts index f023cc0..150f21a 100644 --- a/src/queries/__tests__/MetroStationsQuery.test.ts +++ b/src/queries/__tests__/MetroStationsQuery.test.ts @@ -21,10 +21,8 @@ const GET_METRO_STATIONS = gql` describe("metroStations Query", () => { it("Fetches list of metro stations", async () => { - const { server, metroStations } = createTestServer(); - metroStations.get = jest - .fn() - .mockReturnValueOnce(mockMetroStationsAPIResponse); + const { server, metro } = createTestServer(); + metro.get = jest.fn().mockReturnValueOnce(mockMetroStationsAPIResponse); const { query } = createTestClient(server); const res = await query({ diff --git a/src/utils/createTestServer.ts b/src/utils/createTestServer.ts index 2020b43..37dc691 100644 --- a/src/utils/createTestServer.ts +++ b/src/utils/createTestServer.ts @@ -1,27 +1,23 @@ import { ApolloServer } from "apollo-server-lambda"; import schema from "../schema"; -import MetroStationsDataSource from "../datasources/MetroStationsDataSource"; -import MetroLinesDataSource from "../datasources/MetroLinesDataSource"; +import MetroDataSource from "../datasources/MetroDataSource"; interface TestServer { server: ApolloServer; - metroStations: MetroStationsDataSource; - metroLines: MetroLinesDataSource; + metro: MetroDataSource; } const createTestServer = (): TestServer => { - const metroStations = new MetroStationsDataSource(); - const metroLines = new MetroLinesDataSource(); + const metro = new MetroDataSource(); const server = new ApolloServer({ schema, dataSources: () => ({ - metroStations, - metroLines, + metro, }), }); - return { server, metroStations, metroLines }; + return { server, metro }; }; export default createTestServer;