This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
/
gh-helper.test.js
110 lines (97 loc) · 3.58 KB
/
gh-helper.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-env node, jest */
import 'jest-localstorage-mock'
import {
getGithubUrl,
getGithubIssuesUrl,
fetchLocalAccessToken,
setLocalAccessToken,
// triggerLogin,
getUserInfo,
buildIssueTitle,
buildIssueBody,
createIssue,
} from '../../../src/helper/gh-helper'
beforeEach(() => {
global.localStorage.clear()
window.location.assign = jest.fn()
window.fetch = jest.fn()
window.open = jest.fn()
})
describe('gh-helper getGithubUrl', () => {
it('should return github url', () => {
expect(getGithubUrl()).toBe('https://github.com/')
expect(getGithubUrl('wei')).toBe('https://github.com/wei')
expect(getGithubUrl('wei', 'new-issue')).toBe('https://github.com/wei/new-issue')
})
})
describe('gh-helper getGithubIssuesUrl', () => {
it('should return github issues url', () => {
expect(getGithubIssuesUrl()).toBeNull()
expect(getGithubIssuesUrl('wei')).toBeNull()
expect(getGithubIssuesUrl('wei', 'new-issue')).toBe('https://github.com/wei/new-issue/issues')
})
})
describe('gh-helper localAccessToken', () => {
it('should set localAccessToken', () => {
let KEY = 'GITHUB_ACCESS_TOKEN'
let TOKEN = 'test_token'
expect(fetchLocalAccessToken()).toBe('')
setLocalAccessToken(TOKEN)
expect(global.localStorage.setItem).toBeCalledWith(KEY, TOKEN)
expect(fetchLocalAccessToken()).toBe(TOKEN)
expect(global.localStorage.getItem).toBeCalledWith(KEY)
})
})
describe('gh-helper triggerLogin', () => {
it('should trigger login', () => {
// TODO Mock request
window.location.host = 'new-issue.org'
})
})
describe('gh-helper getUserInfo', () => {
it('should getUserInfo no code', () => {
setLocalAccessToken('')
return getUserInfo()
.catch(e => { expect(e.message).toBe('No Access Token') })
})
it('should getUserInfo no code', () => {
const TOKEN = 'test_token'
setLocalAccessToken(TOKEN)
// TODO Mock request
})
})
describe('gh-helper buildIssueTitle', () => {
it('should return built issue title', () => {
expect(buildIssueTitle()).toBe('')
expect(buildIssueTitle([{ label: 'label 1\n', value: 'value 1' }])).toMatchSnapshot()
expect(buildIssueTitle([{ label: 'label 1\n', value: 'value 1' }, { label: 'label 2\n', value: 'value 2' }]))
.toMatchSnapshot()
})
})
describe('gh-helper buildIssueBody', () => {
it('should return built issue body', () => {
expect(buildIssueBody())
.toMatchSnapshot()
expect(buildIssueBody([{ label: 'label 1\n', value: 'value 1' }]))
.toMatchSnapshot()
expect(buildIssueBody([{ label: 'label 1\n', value: 'value 1' }, { label: 'label 2\n', value: 'value 2' }]))
.toMatchSnapshot()
expect(buildIssueBody([{}, { label: 'label 1\n', value: null }, { label: 'label 2\n' }, { label: 'label 3\n', value: 'value 3' }]))
.toMatchSnapshot()
expect(buildIssueBody([{ label: 'label 1\n', value: '' }, { label: 'label 2\n', value: 'value 2' }], { hideEmpty: true }))
.toMatchSnapshot()
expect(buildIssueBody([{ label: 'label 1\n' }, { label: 'label 2\n', value: 'value 2' }], { hideEmpty: true }))
.toMatchSnapshot()
expect(buildIssueBody([{ value: 'value 1' }, { label: 'label 2\n', value: 'value 2' }], { hideEmpty: true }))
.toMatchSnapshot()
expect(buildIssueBody([{ label: null, value: 'value 1' }, { label: 'label 2\n', value: 'value 2' }], { hideEmpty: true }))
.toMatchSnapshot()
})
})
describe('gh-helper createIssue', () => {
it('should create issue', async () => {
await expect(createIssue()).rejects.toThrow()
await expect(createIssue('wei')).rejects.toThrow()
// TODO Mock request
})
})