Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation about how testing components wrapped by CASL #216

Closed
albcl opened this issue Aug 19, 2019 · 3 comments
Closed

Documentation about how testing components wrapped by CASL #216

albcl opened this issue Aug 19, 2019 · 3 comments
Labels

Comments

@albcl
Copy link

albcl commented Aug 19, 2019

I am running into some issues when trying to implement simple tests for components wrapped by CASL, and I can't find much information about how to implement those test in this kind of cases.

Here is an example of what's happening to me:

I have some simple components that are just wrapped by a simple <Can I="see" a="post>:

const PostSection = () => (
  <Can I="see" a="post">
    {() => (
      <Section>
        [...]
      </Section>
    )}
  </Can>
);

But when I try to build a simple test such as the following dummy one it fails to pass the condition and the snapshot ends as a simple exports['Can I see a post'] = 'null';

describe('Can I see a post', () => {
  beforeAll(() => {
    ability.update([]);
    const rules = {
      action: 'see',
      subject: 'post',
    };

    ability.update(rules);
  });

  it('Renders just fine', () => {
    const component = renderer.create(<Post />).toJSON();

    expect(component).toMatchSnapshot();
  });
});

I have console logged abilities after updating it and its rules are just empty, but I have implemented it as I do in the app itself. I am missing something here (probably very simple) but I just don't see what it is.

@stalniy
Copy link
Owner

stalniy commented Aug 19, 2019

You passed rules as an object but it expects to receive an array. So, change your beforeAll to this:

beforeAll(()=>{
  ability.update([{ action: “see”, subject: “post” }])
})

@stalniy
Copy link
Owner

stalniy commented Aug 20, 2019

Also I’d not recommend using snapshot testing: https://blog.usejournal.com/whats-wrong-with-snapshot-tests-37fbe20dfe8e

@stalniy stalniy closed this as completed Aug 20, 2019
@albcl
Copy link
Author

albcl commented Aug 21, 2019

You were right. I didn't spot that I was missing the array. That was an easy one, haha.

Thank you very much (great library by the way!)

Note: Thanks for the suggestion about avoiding snapshots. I'll take a look to that link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants