Skip to content

Commit

Permalink
feat: unit tests for RepositoryConfig is done
Browse files Browse the repository at this point in the history
  • Loading branch information
skywarth committed Jun 25, 2024
1 parent 768df91 commit b0ac08d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# darkest-PR

[![codecov](https://codecov.io/gh/skywarth/darkest-PR/graph/badge.svg?token=Z86VA7I4HH)](https://codecov.io/gh/skywarth/darkest-PR)

## Setup

Expand Down
78 changes: 78 additions & 0 deletions test/unit/config/repositoryConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {RepositoryConfig} from "../../../src/Config/RepositoryConfig";
import nock from "nock";
import {Context} from "probot";
import {RequestError} from "@octokit/request-error";
import {EventSubscriptionsDTO} from "../../../src/DTO/EventSubscriptionsDTO";



Expand Down Expand Up @@ -133,5 +134,82 @@ describe('RepositoryConfig', () => {
});
})

describe('isEventSubscriptionEnabled()', () => {
test("No declaration for event subscriptions, using defaults", async () => {
const mockConfig:Partial<RepositoryConfig> = {
debug_mode: true,
emojis: false,
};

const repositoryConfig=new RepositoryConfig(mockConfig);

expect(repositoryConfig.isEventSubscriptionEnabled('issue_comment.created')).toBe(repositoryConfig.defaultEventSubscription);
expect(logErrorSpy).not.toBeCalled();
});

test("Declaration for event subscriptions, but empty", async () => {
const mockConfig:Partial<RepositoryConfig> = {
debug_mode: false,
emojis: true,
event_subscriptions:{}
};

const repositoryConfig=new RepositoryConfig(mockConfig);

expect(repositoryConfig.isEventSubscriptionEnabled('issue_comment.created')).toBe(repositoryConfig.defaultEventSubscription);
expect(logErrorSpy).not.toBeCalled();
});

test("Declaration for event subscriptions, contains valid keys and values", async () => {
const eventSubscriptions:EventSubscriptionsDTO={
'pull_request.opened':true,
'issue_comment.created':false,
'pull_request_review.submitted':false,
};
const mockConfig:Partial<RepositoryConfig> = {
debug_mode: false,
emojis: true,
event_subscriptions:eventSubscriptions
};

const repositoryConfig=new RepositoryConfig(mockConfig);

expect(repositoryConfig.isEventSubscriptionEnabled('issue_comment.created')).toBe(eventSubscriptions['issue_comment.created']);
expect(repositoryConfig.isEventSubscriptionEnabled('pull_request.opened')).toBe(eventSubscriptions['pull_request.opened']);
expect(repositoryConfig.isEventSubscriptionEnabled('pull_request_review.submitted')).toBe(eventSubscriptions['pull_request_review.submitted']);
expect(logErrorSpy).not.toBeCalled();
});

});

describe('Initialization/Constructor', () => {
test("Standard procedure, valid config with event subscriptions", async () => {
const eventSubscriptions:EventSubscriptionsDTO={
'pull_request.opened':true,
'issue_comment.created':false,
'pull_request_review.submitted':false,
};
const mockConfig:Partial<RepositoryConfig> = {
debug_mode: false,
emojis: true,
event_subscriptions:eventSubscriptions
};


getContentsSpy.mockImplementationOnce(async ()=>mockConfigEndpointResponse(JSON.stringify(mockConfig)));

const config = await RepositoryConfig.readConfigFromRepository(mockGitHubContext);
const repositoryConfig=new RepositoryConfig(config);

expect(getContentsSpy).toHaveBeenCalledOnce();
expect(repositoryConfig.debug_mode).toStrictEqual(mockConfig.debug_mode);
expect(repositoryConfig.emojis).toStrictEqual(mockConfig.emojis);
expect(repositoryConfig.event_subscriptions).toStrictEqual(mockConfig.event_subscriptions);
expect(logErrorSpy).not.toBeCalled();


});
});


});

0 comments on commit b0ac08d

Please sign in to comment.