-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81a977e
commit 4134a2d
Showing
33 changed files
with
538 additions
and
40 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...sts__/entities/entity-item.entity.test.ts → ...ommon/entities/entity-item.entity.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
..._tests__/enums/time-duration.enum.test.ts → ..._/common/enums/time-duration.enum.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { | ||
setStorageItem, | ||
getStorageItem, | ||
removeStorageItem, | ||
multiRemoveFromStorage, | ||
} from "react-native/helpers/storage.helper"; | ||
|
||
jest.mock("@react-native-async-storage/async-storage"); | ||
|
||
describe("storage.helper", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("setStorageItem sets an item in AsyncStorage", async () => { | ||
const key = "testKey"; | ||
const value = "testValue"; | ||
|
||
await setStorageItem(key, value); | ||
|
||
expect(AsyncStorage.setItem).toHaveBeenCalledWith(key, value); | ||
}); | ||
|
||
test("getStorageItem gets an item from AsyncStorage", async () => { | ||
const key = "testKey"; | ||
const value = "testValue"; | ||
|
||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(value); | ||
|
||
const result = await getStorageItem(key); | ||
|
||
expect(AsyncStorage.getItem).toHaveBeenCalledWith(key); | ||
expect(result).toBe(value); | ||
}); | ||
|
||
test("removeStorageItem removes an item from AsyncStorage", async () => { | ||
const key = "testKey"; | ||
|
||
await removeStorageItem(key); | ||
|
||
expect(AsyncStorage.removeItem).toHaveBeenCalledWith(key); | ||
}); | ||
|
||
test("multiRemoveFromStorage removes multiple items from AsyncStorage", async () => { | ||
const keys = ["testKey1", "testKey2"]; | ||
|
||
await multiRemoveFromStorage(keys); | ||
|
||
expect(AsyncStorage.multiRemove).toHaveBeenCalledWith(keys); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
__tests__/react-native/hooks/async-storage-cache.hook.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { TimeDurations } from "common/enums/time-duration.enum"; | ||
import { useAsyncStorageCache } from "react-native/hooks/async-storage-cache.hook"; | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
|
||
jest.mock("@react-native-async-storage/async-storage"); | ||
|
||
describe("useAsyncStorageCache", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should set item in cache with expiration", async () => { | ||
const { setItem } = useAsyncStorageCache(); | ||
const key = "test-key"; | ||
const value = "test-value"; | ||
const expiration = TimeDurations.FiveMinutes; | ||
|
||
await setItem(key, value, expiration); | ||
|
||
const calls = (AsyncStorage.setItem as jest.Mock).mock.calls[0]; | ||
const cachedItem = JSON.parse(calls[1]); | ||
|
||
expect(calls[0]).toBe(key); | ||
expect(cachedItem.value).toBe(value); | ||
expect(typeof cachedItem.expireAt).toBe("number"); | ||
}); | ||
|
||
it("should get item from cache if not expired", async () => { | ||
const { getItem } = useAsyncStorageCache(); | ||
const key = "test-key"; | ||
const value = "test-value"; | ||
const expireAt = Date.now() + TimeDurations.FiveMinutes; | ||
|
||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce( | ||
JSON.stringify({ value, expireAt }) | ||
); | ||
|
||
const result = await getItem(key); | ||
|
||
expect(AsyncStorage.getItem).toHaveBeenCalledWith(key); | ||
expect(result).toBe(value); | ||
}); | ||
|
||
it("should return null if item is expired", async () => { | ||
const { getItem } = useAsyncStorageCache(); | ||
const key = "test-key"; | ||
const value = "test-value"; | ||
const expireAt = Date.now() - TimeDurations.FiveMinutes; // Already expired | ||
|
||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce( | ||
JSON.stringify({ value, expireAt }) | ||
); | ||
|
||
const result = await getItem(key); | ||
|
||
expect(AsyncStorage.getItem).toHaveBeenCalledWith(key); | ||
expect(AsyncStorage.removeItem).toHaveBeenCalledWith(key); | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { renderHook, act } from "@testing-library/react-hooks"; | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { useEntityLoader } from "react-native/hooks/entity-loader.hook"; | ||
import { TimeDurations } from "common/enums/time-duration.enum"; | ||
|
||
jest.mock("@react-native-async-storage/async-storage", () => ({ | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
removeItem: jest.fn(), | ||
})); | ||
|
||
const mockFetchFunction = jest.fn(); | ||
|
||
describe("useEntityLoader", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should load data from cache if available", async () => { | ||
const cachedData = [{ id: 1, name: "test" }]; | ||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce( | ||
JSON.stringify({ | ||
value: cachedData, | ||
expireAt: Date.now() + TimeDurations.OneDay, | ||
}) | ||
); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useEntityLoader({ | ||
fetchFunction: mockFetchFunction, | ||
cacheKey: "test-cache", | ||
cacheExpiration: TimeDurations.OneDay, | ||
labelField: "name", | ||
valueField: "id", | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.items).toEqual(cachedData); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(AsyncStorage.getItem).toHaveBeenCalledWith("test-cache"); | ||
}); | ||
|
||
it("should fetch data and set cache if no cached data", async () => { | ||
const fetchedData = [{ id: 1, name: "test" }]; | ||
mockFetchFunction.mockResolvedValueOnce(fetchedData); | ||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(null); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useEntityLoader({ | ||
fetchFunction: mockFetchFunction, | ||
cacheKey: "test-cache", | ||
cacheExpiration: TimeDurations.OneDay, | ||
labelField: "name", | ||
valueField: "id", | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.items).toEqual(fetchedData); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(mockFetchFunction).toHaveBeenCalled(); | ||
expect(AsyncStorage.setItem).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { renderHook, act } from "@testing-library/react-hooks"; | ||
import AsyncStorage from "@react-native-async-storage/async-storage"; | ||
import { useObjectLoader } from "react-native/hooks/object-loader.hook"; | ||
import { TimeDurations } from "common/enums/time-duration.enum"; | ||
|
||
jest.mock("@react-native-async-storage/async-storage", () => ({ | ||
getItem: jest.fn(), | ||
setItem: jest.fn(), | ||
removeItem: jest.fn(), | ||
})); | ||
|
||
const mockFetchObjectFunction = jest.fn(); | ||
|
||
describe("useObjectLoader", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should load object from cache if available", async () => { | ||
const cachedObject = { id: 1, name: "test" }; | ||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce( | ||
JSON.stringify({ | ||
value: cachedObject, | ||
expireAt: Date.now() + TimeDurations.OneDay, | ||
}) | ||
); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useObjectLoader({ | ||
fetchFunction: mockFetchObjectFunction, | ||
cacheKey: "test-cache-object", | ||
cacheExpiration: TimeDurations.OneDay, | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.item).toEqual(cachedObject); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(AsyncStorage.getItem).toHaveBeenCalledWith("test-cache-object"); | ||
}); | ||
|
||
it("should fetch object and set cache if no cached object", async () => { | ||
const fetchedObject = { id: 1, name: "test" }; | ||
mockFetchObjectFunction.mockResolvedValueOnce(fetchedObject); | ||
(AsyncStorage.getItem as jest.Mock).mockResolvedValueOnce(null); | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useObjectLoader({ | ||
fetchFunction: mockFetchObjectFunction, | ||
cacheKey: "test-cache-object", | ||
cacheExpiration: TimeDurations.OneDay, | ||
}) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current.item).toEqual(fetchedObject); | ||
expect(result.current.isLoading).toBe(false); | ||
expect(mockFetchObjectFunction).toHaveBeenCalled(); | ||
expect(AsyncStorage.setItem).toHaveBeenCalled(); | ||
}); | ||
}); |
4 changes: 2 additions & 2 deletions
4
src/__tests__/hooks/cache.hook.test.ts → __tests__/web/hooks/cache.hook.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
..._tests__/hooks/entity-loader.hook.test.ts → ...ts__/web/hooks/entity-loader.hook.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
roots: ["<rootDir>/__tests__/react-native", "<rootDir>/src/react-native"], | ||
transform: { | ||
"^.+\\.tsx?$": "ts-jest", | ||
}, | ||
testRegex: "(/__tests__/react-native/.*|(\\.|/)(test|spec))\\.tsx?$", | ||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], | ||
collectCoverage: true, | ||
coverageDirectory: "coverage/react-native", | ||
moduleNameMapper: { | ||
"^common/(.*)$": "<rootDir>/src/common/$1", | ||
"^react-native/(.*)$": "<rootDir>/src/react-native/$1", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
roots: ["<rootDir>/__tests__/react-native", "<rootDir>/src/react-native"], | ||
transform: { | ||
"^.+\\.tsx?$": "ts-jest", | ||
}, | ||
testRegex: "(/__tests__/react-native/.*|(\\.|/)(test|spec))\\.tsx?$", | ||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], | ||
collectCoverage: true, | ||
coverageDirectory: "coverage/react-native", | ||
moduleNameMapper: { | ||
"^common/(.*)$": "<rootDir>/src/common/$1", | ||
"^react-native/(.*)$": "<rootDir>/src/react-native/$1", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./helpers"; | ||
export * from "./hooks"; | ||
export * from "./enums"; | ||
export * from "./common/helpers"; | ||
export * from "./common/hooks"; | ||
export * from "./common/enums"; |
Oops, something went wrong.