-
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.
chore: Added redux, updated test renderer.
- Loading branch information
Showing
15 changed files
with
1,581 additions
and
743 deletions.
There are no files selected for viewing
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,2 @@ | ||
{ | ||
} |
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,11 +1,11 @@ | ||
import { render } from "@testing-library/react-native"; | ||
import React from "react"; | ||
import renderer from "react-test-renderer"; | ||
|
||
import App from "./App"; | ||
|
||
describe("<App />", () => { | ||
it("has 1 child", () => { | ||
const tree: any = renderer.create(<App />).toJSON(); | ||
expect(tree.children.length).toBe(1); | ||
it("Has expected number of children.", () => { | ||
const tree: any = render(<App />).toJSON(); | ||
expect(tree.children.length).toBe(2); | ||
}); | ||
}); |
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,49 @@ | ||
//import { screen, act } from "@testing-library/react"; | ||
import { render, screen, act } from "@testing-library/react-native"; | ||
import React from "react"; | ||
import { Provider } from "react-redux"; | ||
|
||
import TimeInApp from "."; | ||
import { store } from "../../store"; | ||
describe("<TimeInApp />", () => { | ||
it("increments the timer each second", () => { | ||
render( | ||
<Provider store={store}> | ||
<TimeInApp /> | ||
</Provider>, | ||
); | ||
expect(screen.queryByText("0")).toBeTruthy(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1000); | ||
}); | ||
expect(screen.queryByText("1")).toBeTruthy(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1000); | ||
}); | ||
expect(screen.queryByText("2")).toBeTruthy(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(500); | ||
}); | ||
expect(screen.queryByText("2")).toBeTruthy(); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(500); | ||
}); | ||
expect(screen.queryByText("3")).toBeTruthy(); | ||
}); | ||
|
||
it("clears the interval when the component is unmounted", () => { | ||
const clearIntervalMock = jest.spyOn(window, "clearInterval"); | ||
const { unmount } = render( | ||
<Provider store={store}> | ||
<TimeInApp /> | ||
</Provider>, | ||
); | ||
|
||
unmount(); | ||
expect(clearIntervalMock).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,21 @@ | ||
import { Text } from "@rneui/themed"; | ||
import React, { useEffect } from "react"; | ||
|
||
import { useAppSelector, useAppDispatch } from "../../store/hooks"; | ||
import { increment, selectCount } from "../../store/timeInAppSlice"; | ||
|
||
// This component will show the time in seconds that the user has been in the app. | ||
export default function TimeInApp() { | ||
const timeInApp = useAppSelector(selectCount); | ||
const dispatch = useAppDispatch(); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
dispatch(increment()); | ||
}, 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [dispatch]); | ||
|
||
return <Text>{timeInApp}</Text>; | ||
} |
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
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,9 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
import { useDispatch, useSelector, useStore } from "react-redux"; | ||
|
||
import type { AppDispatch, AppStore, RootState } from "../store"; | ||
|
||
// Use these for typescript instead of plain hooks. | ||
export const useAppDispatch = useDispatch.withTypes<AppDispatch>(); | ||
export const useAppSelector = useSelector.withTypes<RootState>(); | ||
export const useAppStore = useStore.withTypes<AppStore>(); |
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 @@ | ||
import { configureStore } from "@reduxjs/toolkit"; | ||
|
||
import timeInAppReducer from "./timeInAppSlice"; | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
timeInApp: timeInAppReducer, | ||
}, | ||
}); | ||
|
||
// Export types from our store. | ||
export type AppStore = typeof store; | ||
export type RootState = ReturnType<AppStore["getState"]>; | ||
export type AppDispatch = AppStore["dispatch"]; |
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,34 @@ | ||
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; | ||
|
||
import type { RootState } from "."; | ||
|
||
// Typing for `state`. | ||
interface timeInAppState { | ||
value: number; | ||
} | ||
const initialState: timeInAppState = { | ||
value: 0, | ||
}; | ||
|
||
// Slice definition. | ||
export const timeInAppSlice = createSlice({ | ||
name: "timeInApp", | ||
initialState, | ||
reducers: { | ||
increment: (state) => { | ||
state.value += 1; | ||
}, | ||
decrement: (state) => { | ||
state.value -= 1; | ||
}, | ||
incrementByAmount: (state, action: PayloadAction<number>) => { | ||
state.value += action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
// Exports. | ||
export const { increment, decrement, incrementByAmount } = | ||
timeInAppSlice.actions; | ||
export const selectCount = (state: RootState) => state.timeInApp.value; | ||
export default timeInAppSlice.reducer; |
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
Oops, something went wrong.