Skip to content

Commit c762915

Browse files
committed
fix: main tests and mocks
1 parent 18794ea commit c762915

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

build/blogpub.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function loadArticleFile(
2525
repo,
2626
ref: context.sha,
2727
})
28-
).data;
28+
).data;
2929
const articleFileRegex = new RegExp(`${folderName}\/.*\.md`);
3030
const mdFiles = commit.files!.filter((f) => articleFileRegex.test(f.filename!));
3131
core.debug(`Found ${mdFiles.length} markdown files`);
@@ -50,7 +50,6 @@ async function fileContentExists(github: Github, filePath: string, ref: string):
5050
});
5151
return res.status === 200;
5252
} catch (e) {
53-
console.log(e);
5453
return false;
5554
}
5655
}
@@ -73,10 +72,7 @@ export async function run() {
7372
articleFile.fileName,
7473
beforeCommit,
7574
);
76-
// NOTE: the return is not being recognized in the coverage, hence the ignore statements
77-
/* istanbul ignore next */
7875
if (articleAlreadyExists) {
79-
/* istanbul ignore next */
8076
core.debug(`Article ${articleFile.fileName} already published. Skipping.`);
8177
return;
8278
}

test/main.spec.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const octokitMock = {
1313
rest: {
1414
repos: {
1515
getContent: jest.fn(),
16-
}
17-
}
16+
},
17+
},
1818
};
1919
jest.mock('fs', () => ({
2020
promises: {
@@ -36,15 +36,14 @@ jest.mock('@actions/github', () => ({
3636
repo: 'repo',
3737
},
3838
payload: {
39-
before: 'sha-before'
39+
before: 'sha-before',
4040
},
4141
sha: 'commit-sha',
4242
},
4343
getOctokit: jest.fn(),
4444
}));
4545
jest.mock('@actions/core');
4646

47-
4847
describe('blogpub', () => {
4948
beforeEach(jest.clearAllMocks);
5049

@@ -108,19 +107,22 @@ describe('blogpub', () => {
108107
expect(promises.readFile).toHaveBeenCalledWith('./blogs/blog-01.md', 'utf8');
109108
expect(core.setFailed).toHaveBeenCalledWith(err);
110109
});
111-
110+
112111
it('should skip if file already exists in repository', async () => {
113112
(core.getInput as jest.Mock).mockImplementation((key: string) => {
114113
return key === 'articles_folder' ? 'blogs' : '';
115114
});
115+
(promises.readFile as jest.Mock).mockResolvedValue('content');
116+
octokitMock.request.mockResolvedValue(fileData);
116117
octokitMock.rest.repos.getContent.mockResolvedValue({
117118
status: 200,
118119
});
119-
octokitMock.request.mockResolvedValueOnce({ status: 200 });
120+
120121
await run();
121122
expect(parseArticle).not.toHaveBeenCalled();
122123
expect(octokitMock.rest.repos.getContent).toHaveBeenCalledWith({
123124
owner: 'owner',
125+
path: 'blogs/blog-01.md',
124126
repo: 'repo',
125127
ref: 'sha-before',
126128
});
@@ -133,12 +135,12 @@ describe('blogpub', () => {
133135

134136
const err = new Error('parseArticle');
135137

138+
octokitMock.request.mockResolvedValue(fileData);
139+
octokitMock.rest.repos.getContent.mockRejectedValue({ status: 404 });
136140
(promises.readFile as jest.Mock).mockResolvedValue('content');
137141
(parseArticle as jest.Mock).mockImplementation(() => {
138142
throw err;
139143
});
140-
octokitMock.request.mockResolvedValueOnce(fileData);
141-
octokitMock.rest.repos.getContent.mockResolvedValueOnce({ status: 404 });
142144

143145
await run();
144146

@@ -164,14 +166,14 @@ describe('blogpub', () => {
164166
});
165167
const err = new Error('createArticle');
166168

169+
octokitMock.request.mockResolvedValue(fileData);
170+
octokitMock.rest.repos.getContent.mockRejectedValue({ status: 404 });
167171
(promises.readFile as jest.Mock).mockResolvedValue('content');
168172
(parseArticle as jest.Mock).mockReturnValue({
169173
config: { title: 'New' },
170174
content: 'parsed',
171175
});
172176
(medium.createArticle as jest.Mock).mockRejectedValue(err);
173-
octokitMock.request.mockResolvedValueOnce(fileData);
174-
octokitMock.rest.repos.getContent.mockResolvedValueOnce({ status: 404 });
175177

176178
await run();
177179

@@ -188,24 +190,26 @@ describe('blogpub', () => {
188190
const template = jest.fn(() => 'compiled');
189191
(Handlebars.compile as jest.Mock).mockReturnValue(template);
190192

191-
(core.getInput as jest.Mock).mockReturnValueOnce('github-token');
193+
(core.getInput as jest.Mock).mockImplementation((key: string) => {
194+
return key === 'articles_folder' ? 'blogs' : '';
195+
});
192196
(promises.readFile as jest.Mock).mockResolvedValue('content');
193197
(parseArticle as jest.Mock).mockReturnValue({
194198
config: { title: 'New' },
195199
content: 'parsed',
196200
});
197201
(devto.createArticle as jest.Mock).mockRejectedValue(new Error(''));
198202
(medium.createArticle as jest.Mock).mockResolvedValue({ url: 'medium.com/new' });
199-
octokitMock.request.mockResolvedValueOnce(fileData);
200-
octokitMock.request.mockResolvedValueOnce({ status: 404 });
203+
octokitMock.request.mockResolvedValue(fileData);
204+
octokitMock.rest.repos.getContent.mockRejectedValue({ status: 404 });
201205

202206
await run();
203207

204-
expect(getOctokit).toHaveBeenCalledWith('github-token');
205208
expect(core.getInput).toHaveBeenCalledWith('gh_token', { required: true });
206209
expect(core.getInput).toHaveBeenCalledWith('medium_token', { required: true });
207210
expect(core.getInput).toHaveBeenCalledWith('medium_user_id', { required: true });
208211
expect(core.getInput).toHaveBeenCalledWith('medium_base_url', { required: false });
212+
expect(medium.createArticle).toHaveBeenCalled();
209213
expect(core.setOutput).toHaveBeenCalledWith('medium_url', 'medium.com/new');
210214
});
211215

@@ -232,8 +236,8 @@ describe('blogpub', () => {
232236
});
233237
(medium.createArticle as jest.Mock).mockResolvedValue({ url: 'medium.com/new' });
234238
(devto.createArticle as jest.Mock).mockRejectedValue(err);
235-
octokitMock.request.mockResolvedValueOnce(fileData);
236-
octokitMock.request.mockResolvedValueOnce({ status: 404 });
239+
octokitMock.request.mockResolvedValue(fileData);
240+
octokitMock.rest.repos.getContent.mockRejectedValue({ status: 404 });
237241

238242
await run();
239243

@@ -266,8 +270,8 @@ describe('blogpub', () => {
266270
});
267271
(medium.createArticle as jest.Mock).mockResolvedValue({ url: 'medium.com/new' });
268272
(devto.createArticle as jest.Mock).mockResolvedValue({ url: 'dev.to/new' });
269-
octokitMock.request.mockResolvedValueOnce(fileData);
270-
octokitMock.request.mockResolvedValueOnce({ status: 404 });
273+
octokitMock.request.mockResolvedValue(fileData);
274+
octokitMock.rest.repos.getContent.mockRejectedValue({ status: 404 });
271275

272276
await run();
273277

@@ -278,6 +282,8 @@ describe('blogpub', () => {
278282
});
279283
expect(core.setOutput).toHaveBeenCalledWith('devto_url', 'dev.to/new');
280284
expect(parseArticle).toHaveBeenCalledWith(
281-
'content', 'raw.githubusercontent.com/owner/repo/main/blogs/');
285+
'content',
286+
'raw.githubusercontent.com/owner/repo/main/blogs/',
287+
);
282288
});
283289
});

0 commit comments

Comments
 (0)