Skip to content

Commit 5718934

Browse files
committed
test: add tests cases for update-github-orgs workflow
1 parent cc06752 commit 5718934

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`list - Non-Interactive Mode Should provide a list of available workflows 1`] = `
4+
[
5+
{
6+
"description": "Check the organizations stored and update the information with the GitHub API.",
7+
"name": "update-github-orgs",
8+
},
9+
]
10+
`;

__tests__/cli/workflows.test.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const inquirer = require('inquirer').default
2+
const knexInit = require('knex')
3+
const { getConfig } = require('../../src/config')
4+
const { runWorkflowCommand, listWorkflowCommand } = require('../../src/cli')
5+
const { resetDatabase, getAllProjects, getAllGithubOrgs, addGithubOrg, addProject } = require('../utils')
6+
const { github } = require('../../src/providers')
7+
const { sampleGithubOrg } = require('../fixtures')
8+
9+
const { dbSettings } = getConfig('test')
10+
11+
let knex
12+
13+
beforeAll(() => {
14+
knex = knexInit(dbSettings)
15+
})
16+
beforeEach(async () => {
17+
await resetDatabase(knex)
18+
jest.clearAllMocks()
19+
})
20+
afterEach(jest.clearAllMocks)
21+
afterAll(async () => {
22+
await resetDatabase(knex)
23+
await knex.destroy()
24+
})
25+
26+
describe('list - Non-Interactive Mode', () => {
27+
jest.spyOn(inquirer, 'prompt').mockImplementation(async () => ({}))
28+
29+
test('Should provide a list of available workflows', async () => {
30+
// @TODO: This test can be improved, currently is used to ensure that all the commands are listed
31+
await expect(listWorkflowCommand()).toMatchSnapshot()
32+
})
33+
})
34+
35+
describe('run update-github-orgs - Interactive Mode', () => {
36+
// Mock inquirer for testing
37+
jest.spyOn(inquirer, 'prompt').mockImplementation(async (questions) => {
38+
const questionMap = {
39+
'What is the name of the workflow?': 'update-github-orgs'
40+
}
41+
return questions.reduce((acc, question) => {
42+
acc[question.name] = questionMap[question.message]
43+
return acc
44+
}, {})
45+
})
46+
47+
test('Should throw an error when no Github orgs are stored in the database', async () => {
48+
const projects = await getAllProjects(knex)
49+
expect(projects.length).toBe(0)
50+
const githubOrgs = await getAllGithubOrgs(knex)
51+
expect(githubOrgs.length).toBe(0)
52+
await expect(runWorkflowCommand(knex, {}))
53+
.rejects
54+
.toThrow('No organizations found. Please add organizations/projects before running this workflow.')
55+
})
56+
57+
test('Should update the project with new information available', async () => {
58+
// Prepare the database
59+
await addProject(knex, { name: sampleGithubOrg.login, category: 'impact' })
60+
await addGithubOrg(knex, { login: sampleGithubOrg.login, html_url: sampleGithubOrg.html_url })
61+
const projects = await getAllProjects(knex)
62+
expect(projects.length).toBe(1)
63+
let githubOrgs = await getAllGithubOrgs(knex)
64+
expect(githubOrgs.length).toBe(1)
65+
expect(githubOrgs[0].description).toBe(null)
66+
// Mock the fetchOrgByLogin method
67+
jest.spyOn(github, 'fetchOrgByLogin').mockResolvedValue(sampleGithubOrg)
68+
await runWorkflowCommand(knex, {})
69+
// Check the database changes
70+
githubOrgs = await getAllGithubOrgs(knex)
71+
expect(githubOrgs.length).toBe(1)
72+
expect(githubOrgs[0].description).toBe(sampleGithubOrg.description)
73+
})
74+
75+
test.todo('Should throw an error when the Github API is not available')
76+
})
77+
78+
describe('run update-github-orgs - Non-Interactive Mode', () => {
79+
// Mock inquirer for testing
80+
jest.spyOn(inquirer, 'prompt').mockImplementation(async (questions) => {
81+
const questionMap = {
82+
'What is the name of the workflow?': 'update-github-orgs'
83+
}
84+
return questions.reduce((acc, question) => {
85+
acc[question.name] = questionMap[question.message]
86+
return acc
87+
}, {})
88+
})
89+
90+
test('Should throw an error when invalid name is provided', async () => {
91+
await expect(runWorkflowCommand(knex, { name: 'invented' }))
92+
.rejects
93+
.toThrow('Invalid workflow name. Please enter a valid workflow name.')
94+
})
95+
96+
test('Should throw an error when no name is provided', async () => {
97+
await expect(runWorkflowCommand(knex, { name: undefined }))
98+
.rejects
99+
.toThrow('Invalid workflow name. Please enter a valid workflow name.')
100+
})
101+
102+
test('Should throw an error when no Github orgs are stored in the database', async () => {
103+
const projects = await getAllProjects(knex)
104+
expect(projects.length).toBe(0)
105+
const githubOrgs = await getAllGithubOrgs(knex)
106+
expect(githubOrgs.length).toBe(0)
107+
await expect(runWorkflowCommand(knex, { name: 'update-github-orgs' }))
108+
.rejects
109+
.toThrow('No organizations found. Please add organizations/projects before running this workflow.')
110+
})
111+
112+
test.todo('Should update the project with new information available')
113+
test.todo('Should throw an error when the Github API is not available')
114+
})

0 commit comments

Comments
 (0)