Skip to content

Instantly share code, notes, and snippets.

@MaximeHeckel
Created February 1, 2018 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaximeHeckel/213c02a56940fc030089471c05d4da48 to your computer and use it in GitHub Desktop.
Save MaximeHeckel/213c02a56940fc030089471c05d4da48 to your computer and use it in GitHub Desktop.
Medium - React "sub-components" - Article test file
import React from 'react';
import { mount } from 'enzyme';
import Article from '../';
// First we declare some mocks
const Content = () => <div>[Mock] Content</div>;
const Subtitle = () => <div>[Mock] Subtitle</div>;
const Comments = () => <div>[Mock] Comments</div>;
const Metadata = () => <div>[Mock] Metadata</div>;
const Title = () => <div>[Mock] Title</div>;
const Subtitles = () => <div>[Mock] Subtitles</div>;
it('Renders with all the sub-components', () => {
// Then we try to render the Article component with the desired sub-components
const component = mount(
<Article>
<Article.Title>
<Title />
</Article.Title>
<Article.Subtitle>
<Subtitle />
</Article.Subtitle>
<Article.Metadata>
<Metadata />
</Article.Metadata>
<Article.Content>
<Content />
</Article.Content>
<Article.Comments>
<Comments />
</Article.Comments>
</Article>
);
// Finally we check it matches its snapshot stored in the project
expect(component).toMatchSnapshot();
});
it('Renders with only the Content and Comments', () => {
// We can iterate the same process again with a different combination of sub-components
const component = mount(
<Article>
<Article.Content>
<Content />
</Article.Content>
<Article.Comments>
<Comments />
</Article.Comments>
</Article>
);
expect(component).toMatchSnapshot();
});
it('Renders with a Title and without a subtitle', () => {
const component = mount(
<Article>
<Article.Title>
<Title />
</Article.Title>
<Article.Metadata>
<Metadata />
</Article.Metadata>
<Article.Content>
<Content />
</Article.Content>
<Article.Comments>
<Comments />
</Article.Comments>
</Article>
);
expect(component).toMatchSnapshot();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment