-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeder.test.ts
More file actions
209 lines (164 loc) · 7.01 KB
/
seeder.test.ts
File metadata and controls
209 lines (164 loc) · 7.01 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
jest.mock('../src/seed/github/client');
jest.mock('../src/seed/contentstack/client');
jest.mock('../src/seed/interactive');
jest.mock('tmp');
jest.mock('@contentstack/cli-utilities');
jest.mock('inquirer');
import GitHubClient from '../src/seed/github/client';
import ContentstackClient, { Organization } from '../src/seed/contentstack/client';
import ContentModelSeeder, { ContentModelSeederOptions } from '../src/seed';
import { inquireOrganization, inquireProceed, inquireStack, inquireRepo } from '../src/seed/interactive';
import * as tmp from 'tmp';
import { cliux } from '@contentstack/cli-utilities';
import * as config from './config.json';
const org_name = 'Test Organization';
const org_uid = 'xxxxxxxxxx';
const api_key = config.api_key;
const tmpDirName = '/var/tmp/xxxxxx/';
const repo = 'stack-gatsby-blog';
const options: ContentModelSeederOptions = {
cdaHost: '',
cmaHost: '',
gitHubPath: '',
};
// @ts-ignore
cli = {
debug: jest.fn(),
error: jest.fn(),
action: {
start: jest.fn(),
stop: jest.fn(),
},
};
const mockParsePath = jest.fn().mockReturnValue({
username: 'fakeUserName55',
repo: repo,
});
GitHubClient.parsePath = mockParsePath;
describe('ContentModelSeeder', () => {
beforeEach(() => {
jest.restoreAllMocks();
});
test('should create temp folder and download release', async () => {
// @ts-ignore
const dirSyncMock = jest.spyOn(tmp, 'dirSync').mockReturnValue({
name: tmpDirName,
});
GitHubClient.prototype.getLatest = jest.fn().mockResolvedValue(true);
const seeder = new ContentModelSeeder(options);
const tmpDir = await seeder.downloadRelease();
expect(dirSyncMock).toHaveBeenCalled();
expect(cliux.loader).toHaveBeenCalled();
expect(GitHubClient.prototype.getLatest).toHaveBeenCalledWith(repo, tmpDirName);
expect(cliux.loader).toHaveBeenCalled();
expect(tmpDir).toBe(tmpDirName);
});
test('should automatically proceed when no content types', async () => {
ContentstackClient.prototype.getStack = jest.fn().mockResolvedValue({ master_locale: 'en-us' });
GitHubClient.prototype.getMasterLocaleFromRepo = jest.fn().mockResolvedValue(null);
ContentstackClient.prototype.getContentTypeCount = jest.fn().mockResolvedValue(0);
const seeder = new ContentModelSeeder(options);
const proceed = await seeder.shouldProceed(api_key);
expect(proceed).toBe(true);
});
test('should not proceed when content types exists and user cancels', async () => {
ContentstackClient.prototype.getStack = jest.fn().mockResolvedValue({ master_locale: 'en-us' });
GitHubClient.prototype.getMasterLocaleFromRepo = jest.fn().mockResolvedValue(null);
ContentstackClient.prototype.getContentTypeCount = jest.fn().mockResolvedValue(1);
// @ts-ignore
inquireProceed.mockReturnValue(false);
const seeder = new ContentModelSeeder(options);
const proceed = await seeder.shouldProceed(api_key);
expect(proceed).toBe(false);
});
test('should proceed when content types exists and user accepts risk', async () => {
ContentstackClient.prototype.getStack = jest.fn().mockResolvedValue({ master_locale: 'en-us' });
GitHubClient.prototype.getMasterLocaleFromRepo = jest.fn().mockResolvedValue(null);
ContentstackClient.prototype.getContentTypeCount = jest.fn().mockResolvedValue(1);
// @ts-ignore
inquireProceed.mockReturnValue(true);
const seeder = new ContentModelSeeder(options);
const proceed = await seeder.shouldProceed(api_key);
expect(proceed).toBe(true);
});
test('should skip confirmation when skipStackConfirmation is true', async () => {
ContentstackClient.prototype.getStack = jest.fn().mockResolvedValue({ master_locale: 'en-us' });
GitHubClient.prototype.getMasterLocaleFromRepo = jest.fn().mockResolvedValue(null);
ContentstackClient.prototype.getContentTypeCount = jest.fn().mockResolvedValue(1);
const optionsWithSkip = { ...options, skipStackConfirmation: true };
const seeder = new ContentModelSeeder(optionsWithSkip);
const proceed = await seeder.shouldProceed(api_key);
expect(proceed).toBe(true);
expect(inquireProceed).not.toHaveBeenCalled();
});
test('should create stack', async () => {
ContentstackClient.prototype.createStack = jest.fn().mockResolvedValue({
api_key: api_key,
});
const organization: Organization = {
enabled: true,
name: org_name,
uid: org_uid,
};
const seeder = new ContentModelSeeder(options);
const result = await seeder.createStack(organization, 'Test Stack');
expect(cliux.loader).toHaveBeenCalled();
expect(ContentstackClient.prototype.createStack).toHaveBeenCalled();
expect(cliux.loader).toHaveBeenCalled();
expect(result).toBe(api_key);
});
test('should throw error when user does not have access to any organizations', async () => {
ContentstackClient.prototype.getOrganizations = jest.fn().mockResolvedValue([]);
try {
const seeder = new ContentModelSeeder(options);
await seeder.getInput();
throw new Error('Failed');
} catch (error) {
expect(error.message).toMatch(/You do not have access/gi);
}
});
test('should throw error when template folder does not exist in github', async () => {
ContentstackClient.prototype.getOrganizations = jest.fn().mockResolvedValue([{ uid: org_uid }]);
GitHubClient.prototype.checkIfRepoExists = jest.fn().mockResolvedValue(false);
const seeder = new ContentModelSeeder(options);
await seeder.getInput();
expect(cliux.error).toHaveBeenCalled();
});
test('should prompt for input when organizations and github folder exists', async () => {
GitHubClient.prototype.checkIfRepoExists = jest.fn().mockResolvedValue(true);
ContentstackClient.prototype.getOrganizations = jest.fn().mockResolvedValue([{ uid: org_uid }]);
ContentstackClient.prototype.getStacks = jest.fn().mockResolvedValue([{ uid: api_key }]);
// @ts-ignore
inquireOrganization.mockReturnValue({ uid: org_uid });
// @ts-ignore
inquireStack.mockReturnValue({ uid: api_key });
const seeder = new ContentModelSeeder(options);
const result = await seeder.getInput();
expect(inquireOrganization).toHaveBeenCalled();
expect(ContentstackClient.prototype.getStacks).toHaveBeenCalledWith(org_uid);
expect(inquireStack).toHaveBeenCalled();
expect(result).toHaveProperty('organizationResponse');
expect(result).toHaveProperty('stackResponse');
});
test('should test inquire GitHub repo and filter out not stacks', async () => {
const repos = [
{
name: 'stack-this-is-a-test',
html_url: 'account/stack-this-is-a-test',
},
{
name: 'stack-this-is-cool',
html_url: 'account/stack-this-is-cool',
},
{
name: 'ignore-this',
html_url: 'account/ignore-this',
},
];
// @ts-ignore
GitHubClient.prototype.getAllRepos = jest.fn().mockResolvedValue(repos);
const seeder = new ContentModelSeeder(options);
await seeder.inquireGitHubRepo();
expect(inquireRepo).toBeCalled();
});
});