Skip to content

Commit

Permalink
chore: add basic test suite for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
swain committed Nov 7, 2023
1 parent 6327d85 commit ed943ca
Show file tree
Hide file tree
Showing 5 changed files with 1,612 additions and 20 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/validate-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,30 @@ jobs:

- name: Verify no files have changed after auto-fix
run: git diff --exit-code HEAD

test:
name: Test JS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore node_modules from cache
uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install node_modules
run: yarn install --frozen-lockfile

- name: Install node_modules (example/)
run: yarn install --frozen-lockfile --cwd example

- name: Run Jest
run: yarn test
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"lint": "eslint \"**/*.{js,ts,tsx}\"",
"lint-ci": "yarn lint -f ./node_modules/@firmnav/eslint-github-actions-formatter/dist/formatter.js",
"lint-cpp": "scripts/clang-format.sh",
"test": "jest",
"prepare": "git submodule update --init --recursive && bob build",
"prepack": "bob build",
"update-submodule": "git submodule update --remote --merge",
Expand Down Expand Up @@ -61,16 +62,22 @@
"@react-native-community/eslint-config": "^3.2.0",
"@react-native-community/eslint-plugin": "^1.3.0",
"@release-it/conventional-changelog": "^5.1.1",
"@testing-library/react-native": "^12.3.2",
"@tsconfig/react-native": "^2.0.3",
"@types/jest": "^29.5.7",
"@types/react": "^18.0.34",
"@types/react-test-renderer": "^18.0.5",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"eslint": "^8.38.0",
"eslint-plugin-jest": "^27.2.1",
"jest": "^29.7.0",
"metro-react-native-babel-preset": "^0.77.0",
"prettier": "^2.8.7",
"react": "^18.2.0",
"react-native": "^0.71.6",
"react-native-builder-bob": "^0.20.4",
"react-test-renderer": "^18.2.0",
"release-it": "^15.10.1",
"typescript": "^4.9.4"
},
Expand Down
88 changes: 88 additions & 0 deletions test/hooks.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React from 'react';
import { Button, Text } from 'react-native';
import {
act,
fireEvent,
render,
renderHook,
screen,
} from '@testing-library/react-native';
import { MMKV, useMMKVNumber, useMMKVString } from '../src';

const mmkv = new MMKV();

beforeEach(() => {
mmkv.clearAll();
});

test('hooks update when the value is changed directly through the instance', () => {
const { result } = renderHook(() => useMMKVString('string-key', mmkv));

expect(result.current[0]).toBeUndefined();

// First, make a "normal" change
act(() => {
result.current[1]('value 1');
});

expect(result.current[0]).toStrictEqual('value 1');

// Now, make the change directly through the instance.
act(() => {
mmkv.set('string-key', 'value 2');
});
expect(result.current[0]).toStrictEqual('value 2');
});

test('functional updates to hooks', () => {
const Component: React.FC = () => {
const [state, setState] = React.useState(0);
const [value, setValue] = useMMKVNumber('number-key', mmkv);

return (
<>
<Button
testID="button"
title="Double Increment Me"
onPress={() => {
// Increment the state value twice, using the function form of useState.
setState((current) => current + 1);
setState((current) => current + 1);

// Increment the MMKV value twice, using the same function form.
setValue((current) => (current ?? 0) + 1);
setValue((current) => (current ?? 0) + 1);
}}
/>
<Text testID="state-value">State: {state.toString()}</Text>
<Text testID="mmkv-value">MMKV: {(value ?? 0).toString()}</Text>
</>
);
};

render(<Component />);

const button = screen.getByTestId('button');

// Why these assertions:
// https://github.com/mrousavy/react-native-mmkv/issues/599
fireEvent.press(button);
expect(screen.getByTestId('state-value').children).toStrictEqual([
'State: ',
'2',
]);
expect(screen.getByTestId('mmkv-value').children).toStrictEqual([
'MMKV: ',
'2',
]);

fireEvent.press(button);
expect(screen.getByTestId('state-value').children).toStrictEqual([
'State: ',
'4',
]);
expect(screen.getByTestId('mmkv-value').children).toStrictEqual([
'MMKV: ',
'4',
]);
});
Loading

0 comments on commit ed943ca

Please sign in to comment.