Search code examples
jestjsreact-admin

How to unit test permissions in a React Admin controller?


I'm using react-admin and I have a list component that has some conditional display behavior based on permissions.

I'd like to write a simple Jest test that asserts the correct behavior (display the Edit button if admin, hide the Edit button if not admin), but I need to feed some mock records into my list component. How do I pass mock data to my list component?


Solution

  • There is (now) a dedicated paragraph on the documentation about testing permissions-based view: https://marmelab.com/react-admin/UnitTesting.html#testing-permissions

    I also wrote an example of unit test available on the code base: UnitShow.spec.js

     it('should show the user role in the second tab', () => {
        const wrapper = shallow(<UserShow permissions="admin" />);
    
        const tabs = wrapper.find(Tab);
        const fields = tabs.at(1).find(TextField);
    
        expect(fields.at(0).prop('source')).toBe('role');
    });