Comments (8)
I think I found the issue. I had to wrap the waitForNextUpdate into an act just after rendering the component. As there's a state update on mount.
it('should check notifications permission on mount', async () => {
const { result, waitForNextUpdate } = renderHook(() => useNotificationsScreen());
await act(async () => {
await waitForNextUpdate();
});
expect(checkNotifications).toHaveBeenCalled();
expect(result.current.notificationsEnabled).toBe(true);
});
from react-native-permissions.
@ItsMeJules What mock are you using? Are you sure it actually trigger a re-render?
from react-native-permissions.
@zoontek All the mocked implementations are provided in the sample code. Or else I didn't understand your question. I am, when working on the feature, it was re-setting the state and re-rendering my components based on the return value of the custom hook.
from react-native-permissions.
@ItsMeJules Yes, I saw it after. It was poorly formatted and unreadable at first.
First, try to use the repository mock: https://github.com/zoontek/react-native-permissions/blob/master/mock.js
from react-native-permissions.
Reading your code, it's normal, no issue here.
You are using this as a mock:
jest.mock('react-native-permissions', () => ({
checkNotifications: jest.fn(), // ⚠️ sync function, returns void (undefined)
}));
Except you should use this:
jest.mock('react-native-permissions', () => ({
checkNotifications: jest.fn(async () => ({ status: "granted" })),
}));
Then this:
export const useNotificationsScreen = () => {
const { navigate } = useNavigation<NavigationScreenProp<any, any>>();
const [notificationsEnabled, setNotificationsEnabled] = useState(false);
useEffect(() => {
(async () => {
const { status } = await checkNotifications(); // will return undefined, and crash as it cannot be destructured
setNotificationsEnabled(status === 'granted'); // never fired
})();
}, []);
...
}
PS: There's a "Testing with Jest" part in the README that help to setup the mock: https://github.com/zoontek/react-native-permissions?tab=readme-ov-file#testing-with-jest
from react-native-permissions.
@zoontek I have it mocked in my jest-stup.ts
file. As so :
jest.mock('react-native-permissions', () => require('react-native-permissions/mock'));
This is how I call my jest-setup.ts
config file in my jest.config.js
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
I did read the testing with jest part in the README.md and found it a little scarce.
Applying your latest edited reply, I still have the same issue.
jest.mock('react-native-permissions', () => ({
checkNotifications: jest.fn(async () => ({ status: 'granted' })),
}));
describe('useNotificationsScreen', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should check notifications permission on mount', async () => {
jest.mocked(checkNotifications).mockResolvedValue({
status: 'granted',
settings: {},
} as NotificationsResponse); // <- I also tried removing this whole line and still timed out.
const { result, waitForNextUpdate } = renderHook(() => useNotificationsScreen());
await waitForNextUpdate().then(() => {
console.log('waitForNextUpdate');
});
expect(checkNotifications).toHaveBeenCalled();
expect(result.current.notificationsEnabled).toBe(true);
});
from react-native-permissions.
@ItsMeJules Create a repository with this, I will check / fix it.
from react-native-permissions.
Good to know, I'm closing this.
from react-native-permissions.
Related Issues (20)
- [Android]: Failed to build gradle in monorepo. Unable to resolve react-native location in node_modules HOT 3
- iOS Build failed HOT 2
- After user change setting permission to None. Check permission PERMISSIONS.IOS.PHOTO_LIBRARY / MEDIA_LIBRARY got UNAVAILABLE not DENIED HOT 2
- iOS: check location permission returns 'blocked' when permission is granted but location services are disabled HOT 6
- Cannot read properties of undefined (reading 'iosPermissions')
- Syntax Error in Setup? HOT 5
- [Android] App crashed totally after call `request()` function when `minifyEnabled` is `true` HOT 3
- [Android 13-14] App crash at requestNotifications(['alert', 'sound']) HOT 1
- Request APP_TRACKING_TRANSPARENCY always return 'blocked' before user accept request HOT 6
- Notification Permission In Android HOT 3
- I encountered an error message stating 'ENOENT: no such file or directory HOT 4
- AVAudioApplication build fail HOT 3
- Invalid `Podfile` file: undefined method `prepare_react_native_project!' HOT 1
- Unable to detect IOS album permissions HOT 1
- ios.permission.FACE_ID denied HOT 3
- [Android] 'Allow once' permission option always returns granted as permission status even after killing the app and relaunch it again HOT 1
- [Android] 'Don't Allow' permission option always returns denied as permission status and doesn't request the permission again HOT 3
- Crash on Android: Unable to resume activity java.lang.reflect.InvocationTargetException HOT 1
- await request(PERMISSIONS.IOS.PHOTO_LIBRARY); is always unavailable HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-permissions.