From 39d79916bf14dd451a9414929fcf2435f16e519d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tadeusz=20Miesi=C4=85c?= <tadeusz.miesiac@gmail.com>
Date: Wed, 4 Oct 2023 18:50:52 +0800
Subject: [PATCH] refactor(get drugs string with query param): moved repeated
 string to separate function

---
 src/queries/getDrugsStringWithQuery.test.ts | 10 ++++++++++
 src/queries/getDrugsStringWithQuery.ts      |  4 ++++
 src/redux/drugs/drugs.reducers.test.ts      |  8 ++++----
 src/redux/drugs/drugs.thunks.test.ts        |  6 +++---
 src/redux/drugs/drugs.thunks.ts             |  6 ++----
 5 files changed, 23 insertions(+), 11 deletions(-)
 create mode 100644 src/queries/getDrugsStringWithQuery.test.ts
 create mode 100644 src/queries/getDrugsStringWithQuery.ts

diff --git a/src/queries/getDrugsStringWithQuery.test.ts b/src/queries/getDrugsStringWithQuery.test.ts
new file mode 100644
index 00000000..194f6704
--- /dev/null
+++ b/src/queries/getDrugsStringWithQuery.test.ts
@@ -0,0 +1,10 @@
+import { PROJECT_ID } from '@/constants/mapId';
+import { getDrugsStringWithQuery } from './getDrugsStringWithQuery';
+
+describe('getDrugsStringWithQuery', () => {
+  it('should return url string', () => {
+    expect(getDrugsStringWithQuery('aspirin')).toBe(
+      `projects/${PROJECT_ID}/drugs:search?query=aspirin`,
+    );
+  });
+});
diff --git a/src/queries/getDrugsStringWithQuery.ts b/src/queries/getDrugsStringWithQuery.ts
new file mode 100644
index 00000000..527b0484
--- /dev/null
+++ b/src/queries/getDrugsStringWithQuery.ts
@@ -0,0 +1,4 @@
+import { PROJECT_ID } from '@/constants/mapId';
+
+export const getDrugsStringWithQuery = (searchQuery: string): string =>
+  `projects/${PROJECT_ID}/drugs:search?query=${searchQuery}`;
diff --git a/src/redux/drugs/drugs.reducers.test.ts b/src/redux/drugs/drugs.reducers.test.ts
index 4bf96500..465ccf70 100644
--- a/src/redux/drugs/drugs.reducers.test.ts
+++ b/src/redux/drugs/drugs.reducers.test.ts
@@ -1,11 +1,11 @@
 import { HttpStatusCode } from 'axios';
-import { PROJECT_ID } from '@/constants/mapId';
 import { drugsFixture } from '@/models/fixtures/drugFixtures';
 import { mockNetworkResponse } from '@/utils/mockNetworkResponse';
 import {
   ToolkitStoreWithSingleSlice,
   createStoreInstanceUsingSliceReducer,
 } from '@/utils/createStoreInstanceUsingSliceReducer';
+import { getDrugsStringWithQuery } from '@/queries/getDrugsStringWithQuery';
 import { getDrugs } from './drugs.thunks';
 import drugsReducer from './drugs.slice';
 import { DrugsState } from './drugs.types';
@@ -32,7 +32,7 @@ describe('drugs reducer', () => {
   });
   it('should update store after succesfull getDrugs query', async () => {
     mockedAxiosClient
-      .onGet(`projects/${PROJECT_ID}/drugs:search?query=${SEARCH_QUERY}`)
+      .onGet(getDrugsStringWithQuery(SEARCH_QUERY))
       .reply(HttpStatusCode.Ok, drugsFixture);
 
     const { type } = await store.dispatch(getDrugs(SEARCH_QUERY));
@@ -46,7 +46,7 @@ describe('drugs reducer', () => {
 
   it('should update store after failed getDrugs query', async () => {
     mockedAxiosClient
-      .onGet(`projects/${PROJECT_ID}/drugs:search?query=${SEARCH_QUERY}`)
+      .onGet(getDrugsStringWithQuery(SEARCH_QUERY))
       .reply(HttpStatusCode.NotFound, []);
 
     const { type } = await store.dispatch(getDrugs(SEARCH_QUERY));
@@ -60,7 +60,7 @@ describe('drugs reducer', () => {
 
   it('should update store on loading getDrugs query', async () => {
     mockedAxiosClient
-      .onGet(`projects/${PROJECT_ID}/drugs:search?query=${SEARCH_QUERY}`)
+      .onGet(getDrugsStringWithQuery(SEARCH_QUERY))
       .reply(HttpStatusCode.Ok, drugsFixture);
 
     const drugsPromise = store.dispatch(getDrugs(SEARCH_QUERY));
diff --git a/src/redux/drugs/drugs.thunks.test.ts b/src/redux/drugs/drugs.thunks.test.ts
index ee7d1c2e..97402ddd 100644
--- a/src/redux/drugs/drugs.thunks.test.ts
+++ b/src/redux/drugs/drugs.thunks.test.ts
@@ -1,7 +1,7 @@
 import { HttpStatusCode } from 'axios';
-import { PROJECT_ID } from '@/constants/mapId';
 import { drugsFixture } from '@/models/fixtures/drugFixtures';
 import { mockNetworkResponse } from '@/utils/mockNetworkResponse';
+import { getDrugsStringWithQuery } from '@/queries/getDrugsStringWithQuery';
 import {
   ToolkitStoreWithSingleSlice,
   createStoreInstanceUsingSliceReducer,
@@ -21,7 +21,7 @@ describe('drugs thunks', () => {
   describe('getDrugs', () => {
     it('should return data when data response from API is valid', async () => {
       mockedAxiosClient
-        .onGet(`projects/${PROJECT_ID}/drugs:search?query=${SEARCH_QUERY}`)
+        .onGet(getDrugsStringWithQuery(SEARCH_QUERY))
         .reply(HttpStatusCode.Ok, drugsFixture);
 
       const { payload } = await store.dispatch(getDrugs(SEARCH_QUERY));
@@ -29,7 +29,7 @@ describe('drugs thunks', () => {
     });
     it('should return undefined when data response from API is not valid ', async () => {
       mockedAxiosClient
-        .onGet(`projects/${PROJECT_ID}/drugs:search?query=${SEARCH_QUERY}`)
+        .onGet(getDrugsStringWithQuery(SEARCH_QUERY))
         .reply(HttpStatusCode.Ok, { randomProperty: 'randomValue' });
 
       const { payload } = await store.dispatch(getDrugs(SEARCH_QUERY));
diff --git a/src/redux/drugs/drugs.thunks.ts b/src/redux/drugs/drugs.thunks.ts
index c1726ee6..cbe4ecc2 100644
--- a/src/redux/drugs/drugs.thunks.ts
+++ b/src/redux/drugs/drugs.thunks.ts
@@ -1,7 +1,7 @@
 import { z } from 'zod';
+import { getDrugsStringWithQuery } from '@/queries/getDrugsStringWithQuery';
 import { createAsyncThunk } from '@reduxjs/toolkit';
 import { axiosInstance } from '@/services/api/utils/axiosInstance';
-import { PROJECT_ID } from '@/constants/mapId';
 import { Drug } from '@/types/models';
 import { drugSchema } from '@/models/drugSchema';
 import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
@@ -9,9 +9,7 @@ import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
 export const getDrugs = createAsyncThunk(
   'project/getDrugs',
   async (searchQuery: string): Promise<Drug[] | undefined> => {
-    const response = await axiosInstance.get<Drug[]>(
-      `projects/${PROJECT_ID}/drugs:search?query=${searchQuery}`,
-    );
+    const response = await axiosInstance.get<Drug[]>(getDrugsStringWithQuery(searchQuery));
 
     const isDataValid = validateDataUsingZodSchema(response.data, z.array(drugSchema));
 
-- 
GitLab