Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.

Commit 14d6efa

Browse files
authored
test: add extra tests to github provider (#66)
* test: add extra tests to github provider This includes Enzyme and some Jest matchers for Enzyme. * pipeline: disable lts node due to lack of async await support This creates an issue when testing Async components.
1 parent 0cc473f commit 14d6efa

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
language: node_js
33
node_js:
44
- node
5-
- lts/*
65

76
os:
87
- linux
@@ -37,11 +36,11 @@ deploy:
3736
on:
3837
condition: $TRAVIS_OS_NAME = linux
3938
branch: develop
40-
node: lts/*
39+
node: node
4140
- provider: script
4241
skip_cleanup: true
4342
script: npx semantic-release
4443
on:
4544
condition: $TRAVIS_OS_NAME = linux
4645
branch: master
47-
node: lts/*
46+
node: node

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
"@semantic-release/changelog": "^3.0.0",
2828
"codecov": "^3.1.0",
2929
"conventional-changelog-peakfijn": "^0.8.0",
30+
"enzyme": "^3.7.0",
31+
"enzyme-adapter-react-16": "^1.6.0",
3032
"jest-chain": "^1.0.4",
33+
"jest-enzyme": "^7.0.0",
3134
"now": "^11.4.6",
3235
"react-scripts": "2.0.5",
3336
"release-rules-peakfijn": "^0.8.0",

src/providers/github.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ import { createInstance } from 'react-async';
55
*/
66
export const username = process.env.REACT_APP_GITHUB_USERNAME || 'byCedric';
77

8+
/**
9+
* Get all (GitHub) keywords from a text.
10+
* It will find all words starting with `@` or `#` and return then without this character.
11+
*
12+
* @param {string?} text
13+
* @return {string[]}
14+
*/
15+
export const findKeywords = (text) => (
16+
((text || '').match(/[@#]([a-z0-9]+)/gi) || []).map(value => value.substr(1))
17+
);
18+
819
/**
920
* Fetch the user information from the GitHub API.
1021
*
@@ -24,14 +35,3 @@ export const fetchUser = () => (
2435
* @return {React.Component}
2536
*/
2637
export const AsyncUser = createInstance({ promiseFn: fetchUser });
27-
28-
/**
29-
* Get all (GitHub) keywords from a text.
30-
* It will find all words starting with `@` or `#` and return then without this character.
31-
*
32-
* @param {string?} text
33-
* @return {string[]}
34-
*/
35-
export const findKeywords = (text) => (
36-
((text || '').match(/[@#]([a-z0-9]+)/gi) || []).map(value => value.substr(1))
37-
);

src/providers/github.test.js

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
import { findKeywords } from './github';
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import {
4+
username,
5+
findKeywords,
6+
fetchUser,
7+
AsyncUser,
8+
} from './github';
29

310
describe('providers/github', () => {
11+
describe('username', () => {
12+
it('is defined', () => {
13+
expect(username).toBeDefined();
14+
});
15+
});
16+
417
describe('findKeywords', () => {
518
it('finds mentions from text', () => {
619
expect(findKeywords('Something something @github something @bycedric.'))
@@ -27,4 +40,32 @@ describe('providers/github', () => {
2740
expect(findKeywords('Something something something.')).toHaveLength(0);
2841
});
2942
});
43+
44+
describe('fetchUser', () => {
45+
it('fetches the github user data', async () => {
46+
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: () => ({ username }) });
47+
48+
expect(await fetchUser()).toEqual({ username });
49+
expect(fetch).toHaveBeenCalledWith(`https://api.github.com/users/${username}`);
50+
});
51+
});
52+
53+
describe('AsyncUser', () => {
54+
it('fetches the github user data with a component', async () => {
55+
const promise = Promise.resolve({ ok: true, json: () => ({ username }) });
56+
57+
global.fetch = jest.fn(() => promise);
58+
59+
const component = mount(
60+
<AsyncUser>
61+
{state => (state.data && state.data.username) || null}
62+
</AsyncUser>
63+
);
64+
65+
expect(component).toBeEmptyRender();
66+
await promise;
67+
expect(component).toIncludeText(username);
68+
expect(fetch).toHaveBeenCalledWith(`https://api.github.com/users/${username}`);
69+
});
70+
});
3071
});

src/setupTests.js

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
import 'jest-chain';
2+
import 'jest-enzyme';
3+
import Enzyme from 'enzyme';
4+
import Adapter from 'enzyme-adapter-react-16';
5+
6+
Enzyme.configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)