Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: latest
cache: npm

- name: Setup EAS
Expand All @@ -31,5 +31,8 @@ jobs:
- name: Install dependencies
run: npm install

- name: Run tests
run: npm test

- name: Publish update
run: eas update --auto
44 changes: 23 additions & 21 deletions hooks/__tests__/useReverseGeocoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// useReverseGeocoding.test.ts

import { cleanup, renderHook } from "@testing-library/react-hooks";
import { cleanup, renderHook, waitFor } from "@testing-library/react-native";
import * as ExpoLocation from "expo-location";
import { useReverseGeocoding } from "../useReverseGeocoding";

Expand Down Expand Up @@ -30,11 +28,12 @@ describe("useReverseGeocoding", () => {
mockGeocodedLocation,
]);

const { result, waitForNextUpdate } = renderHook(() =>
useReverseGeocoding(mockLocation)
);
const { result } = renderHook(() => useReverseGeocoding(mockLocation));

await waitForNextUpdate();
const initialValue = result.current;
await waitFor(() => {
expect(result.current).not.toBe(initialValue);
});

expect(result.current.geocodedLocation).toEqual(mockGeocodedLocation);
expect(result.current.error).toBeNull();
Expand All @@ -50,14 +49,12 @@ describe("useReverseGeocoding", () => {
new Error("Failed to fetch location")
);

const { result, waitForNextUpdate } = renderHook(() =>
useReverseGeocoding(mockLocation)
);
const { result } = renderHook(() => useReverseGeocoding(mockLocation));

await waitForNextUpdate();

expect(result.current.geocodedLocation).toBeNull();
expect(result.current.error).toEqual("Failed to fetch location");
await waitFor(() => {
expect(result.current.geocodedLocation).toBeNull();
expect(result.current.error).toEqual("Failed to fetch location");
});
});

it("runs again when latitude or longitude is changed", async () => {
Expand All @@ -71,7 +68,7 @@ describe("useReverseGeocoding", () => {
longitude: -118.2437,
};

const { result, rerender, waitForNextUpdate } = renderHook(
const { result, rerender } = renderHook(
({ location }) => useReverseGeocoding(location),
{ initialProps: { location: initialLocation } }
);
Expand All @@ -88,13 +85,18 @@ describe("useReverseGeocoding", () => {
mockGeocodedLocation,
]);

await waitForNextUpdate();
const initialValue = result.current;
await waitFor(() => {
expect(result.current).not.toBe(initialValue);
});

(ExpoLocation.reverseGeocodeAsync as jest.Mock).mockClear();

rerender({ location: newLocation });

await waitForNextUpdate();
await waitFor(() => {
expect(result.current).not.toBe(initialValue);
});

expect(ExpoLocation.reverseGeocodeAsync).toHaveBeenCalledTimes(1);
expect(ExpoLocation.reverseGeocodeAsync).toHaveBeenCalledWith(newLocation);
Expand All @@ -106,11 +108,11 @@ describe("useReverseGeocoding", () => {
longitude: -122.4194,
};

const { waitForNextUpdate } = renderHook(() =>
useReverseGeocoding(location)
);
const { result } = renderHook(() => useReverseGeocoding(location));

await waitForNextUpdate();
await waitFor(() => {
expect(result.current).not.toBe(location);
});

expect(ExpoLocation.reverseGeocodeAsync).toHaveBeenCalledTimes(1);
expect(ExpoLocation.reverseGeocodeAsync).toHaveBeenCalledWith(location);
Expand Down
39 changes: 27 additions & 12 deletions hooks/__tests__/useWeatherForLocation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// useWeatherForLocation.test.ts

import { renderHook } from "@testing-library/react-hooks";
import { renderHook, waitFor } from "@testing-library/react-native";
import { WeatherError, useWeatherForLocation } from "../useWeatherForLocation";

// Mock the global fetch function
Expand All @@ -25,11 +25,12 @@ describe("useWeatherForLocation", () => {
json: () => Promise.resolve(mockWeatherData),
});

const { result, waitForNextUpdate } = renderHook(() =>
useWeatherForLocation(mockLocation)
);
const { result } = renderHook(() => useWeatherForLocation(mockLocation));

await waitForNextUpdate();
const initialValue = result.current;
await waitFor(() => {
expect(result.current).not.toBe(initialValue);
});

expect(result.current.weatherData).toEqual(mockWeatherData);
expect(result.current.error).toBeNull();
Expand All @@ -46,11 +47,12 @@ describe("useWeatherForLocation", () => {

(fetch as jest.Mock).mockRejectedValue(new Error(mockError.message));

const { result, waitForNextUpdate } = renderHook(() =>
useWeatherForLocation(mockLocation)
);
const { result } = renderHook(() => useWeatherForLocation(mockLocation));

await waitForNextUpdate();
const initialValue = result.current;
await waitFor(() => {
expect(result.current).not.toBe(initialValue);
});

expect(result.current.weatherData).toBeNull();
expect(result.current.error).toEqual(mockError);
Expand All @@ -68,18 +70,31 @@ describe("useWeatherForLocation", () => {
longitude: -118.2437,
};

const { rerender, waitForNextUpdate } = renderHook(
const mockWeatherData = {
weather: "sunny",
};

const { rerender, result } = renderHook(
({ location }) => useWeatherForLocation(location),
{ initialProps: { location: initialLocation } }
);

await waitForNextUpdate();
const initialValue = initialLocation;
await waitFor(() => {
expect(result.current).not.toBe(initialValue);
});

(fetch as jest.Mock).mockClear();

(fetch as jest.Mock).mockResolvedValue({
json: () => Promise.resolve(mockWeatherData),
});

rerender({ location: newLocation });

await waitForNextUpdate();
await waitFor(() => {
expect(result.current.weatherData).toBe(mockWeatherData);
});

expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(
Expand Down
Loading