Skip to content

Commit

Permalink
Add tests for expandOrganizationTeams (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Jun 20, 2023
1 parent 76a42d8 commit 6932662
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const expandOrganizationTeams = async (
name.startsWith(`${context.organization}${ORG_TEAM_SEP}`),
);
// For each team in usersAndTeamsCopy, add the members of the team to the list
for (const teamName in teamNames) {
for (const teamName of teamNames) {
const teamMembers = await context.github.teams.listMembersInOrg({
org: context.organization,
team_slug: teamName.split(ORG_TEAM_SEP)[1],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as assert from 'assert';
import { mockWebhookContext } from '../../../../utils/test_context';
import { expandOrganizationTeams } from '../../../../../services/bots/src/github-webhook/utils/organization_teams';
import { WebhookContext } from 'services/bots/src/github-webhook/github-webhook.model';

describe('expandOrganizationTeams', () => {
let context: WebhookContext<any>;
beforeEach(() => {
context = mockWebhookContext({
payload: { repository: { name: 'test', owner: { login: 'example' } } },
});
});
it('Resolve both user and team', async () => {
// @ts-expect-error mocked function
context.github.teams.listMembersInOrg.mockReturnValueOnce({
data: [{ login: 'user1' }, { login: 'user2' }],
});
assert.deepStrictEqual(await expandOrganizationTeams(context, ['@test', '@example/test']), [
'test',
'example/test',
'user1',
'user2',
]);
expect(context.github.teams.listMembersInOrg).toBeCalledTimes(1);
expect(context.github.teams.listMembersInOrg).toHaveBeenCalledWith({
org: 'example',
team_slug: 'test',
});
});

it('Ensure lowercase', async () => {
assert.deepStrictEqual(await expandOrganizationTeams(context, ['@TEST']), ['test']);
expect(context.github.teams.listMembersInOrg).not.toHaveBeenCalled();
});
});
3 changes: 3 additions & 0 deletions tests/utils/test_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const mockWebhookContext = <T>(params: Partial<WebhookContext<T>>): Webho
removeLabel: jest.fn(),
removeAssignees: jest.fn(),
},
teams: {
listMembersInOrg: jest.fn(),
}
},
{ ...params?.github },
),
Expand Down

0 comments on commit 6932662

Please sign in to comment.