Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests 🧪 #190

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions test/integration/fixtures/metadata.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,22 @@ import {
PullRequestSynchronizeEvent,
} from '@octokit/webhooks-types';

import { events } from '../../../src/events';

import payloadPullRequestOpened from '../fixtures/payloads/pull-request-opened.json';
import payloadPullRequestReopened from '../fixtures/payloads/pull-request-reopened.json';
import payloadPullRequestSynchronize from '../fixtures/payloads/pull-request-synchronize.json';

export interface IMetadataTestContext {
payloads: {
type: (typeof events.pull_request)[number];
payload:
| PullRequestOpenedEvent
| PullRequestReopenedEvent
| PullRequestSynchronizeEvent;
}[];
payloads: (
| PullRequestOpenedEvent
| PullRequestReopenedEvent
| PullRequestSynchronizeEvent
)[];
}

export const metadataContextFixture: IMetadataTestContext = {
payloads: [
{
type: 'pull_request.opened',
payload: payloadPullRequestOpened as PullRequestOpenedEvent,
},
{
type: 'pull_request.reopened',
payload: payloadPullRequestReopened as PullRequestReopenedEvent,
},
{
type: 'pull_request.synchronize',
payload: payloadPullRequestSynchronize as PullRequestSynchronizeEvent,
},
payloadPullRequestOpened as PullRequestOpenedEvent,
payloadPullRequestReopened as PullRequestReopenedEvent,
payloadPullRequestSynchronize as PullRequestSynchronizeEvent,
],
};
60 changes: 60 additions & 0 deletions test/unit/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
configContextFixture,
IConfigTestContext,
} from './fixtures/config.fixture';
import { CustomOctokit } from '../../src/octokit';

describe('Config Object', () => {
beforeEach<IConfigTestContext>(context => {
Expand All @@ -29,6 +30,65 @@ describe('Config Object', () => {
});
});

test('getConfig()', async () => {
process.env['INPUT_CONFIG-PATH'] = '.github/development-freeze.yml';
process.env['GITHUB_REPOSITORY'] = 'test/test';

const configObject = {
policy: [
{
tags: ['alpha', 'beta'],
feedback: {
'frozen-state': 'This is No-No',
'unfreeze-state': 'This is Yes-Yes',
},
random: 'random',
},
],
};

const noConfigObject = undefined;

const octokit = (data: unknown) => {
return {
config: {
get: async (options: {
owner: string;
repo: string;
path: string;
}) => {
return {
config: data,
files: [options.path],
};
},
},
} as unknown as CustomOctokit;
};

let config = await Config.getConfig(octokit(configObject));
expect(config.policy).toMatchInlineSnapshot(`
[
{
"feedback": {
"frozen-state": "This is No-No",
"unfreeze-state": "This is Yes-Yes",
},
"tags": [
"alpha",
"beta",
],
},
]
`);

await expect(
Config.getConfig(octokit(noConfigObject))
).rejects.toThrowErrorMatchingInlineSnapshot(
"\"Missing configuration. Please setup 'devel-freezer' Action using '.github/development-freeze.yml' file.\""
);
});

test<IConfigTestContext>('is config empty', context => {
context.configs.map(async item =>
expect(Config.isConfigEmpty(item)).toEqual(false)
Expand Down
20 changes: 0 additions & 20 deletions test/unit/fixtures/metadata.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Metadata } from '../../../src/metadata';

export interface IMetadataTestContext {
metadata: Metadata[];
invalid: Metadata[];
}

const controller = {} as MetadataController;
Expand All @@ -16,23 +15,4 @@ export const metadataContextFixture: IMetadataTestContext = {
new Metadata(0, controller, { tag: undefined, commentID: '123456789' }),
new Metadata(0, controller, { tag: 'v1', commentID: '123456789' }),
],

invalid: [
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata(),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata(null),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata(undefined),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata(''),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata({}),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata([]),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata({ tag: null, commentID: null }),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Metadata({ tag: undefined, commentID: 123456789 }),
],
};
14 changes: 0 additions & 14 deletions test/unit/fixtures/tag.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Tag } from '../../../src/tag';
export interface ITagTestContext {
tags: Tag[];
tagPolicy: string[][];
invalid: Tag[];
}

export const tagContextFixture: ITagTestContext = {
Expand All @@ -19,17 +18,4 @@ export const tagContextFixture: ITagTestContext = {
],

tagPolicy: [['^S*-rcd$'], ['alpha', 'beta'], ['latest']],

invalid: [
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Tag(),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Tag(null),
new Tag(undefined),
new Tag(''),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Tag({}),
// @ts-expect-error: Let's ignore a type error, it's required for testing
new Tag([]),
],
};
3 changes: 0 additions & 3 deletions test/unit/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import {
describe('Metadata Object', () => {
beforeEach<IMetadataTestContext>(context => {
context.metadata = metadataContextFixture.metadata;
context.invalid = metadataContextFixture.invalid;
});

it<IMetadataTestContext>('can be instantiated', context =>
context.metadata.map(metadataItem => expect(metadataItem).toBeDefined()));

it.todo<IMetadataTestContext>('is invalid');

test<IMetadataTestContext>('get tag()', context =>
context.metadata.map(metadataItem =>
expect(metadataItem.tag).toMatchSnapshot()
Expand Down
9 changes: 0 additions & 9 deletions test/unit/tag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@ describe('Tag Object', () => {
beforeEach<ITagTestContext>(context => {
context.tags = tagContextFixture.tags;
context.tagPolicy = tagContextFixture.tagPolicy;
context.invalid = tagContextFixture.invalid;
});

it<ITagTestContext>('can be instantiated', context =>
context.tags.map(tagsItem => expect(tagsItem).toBeDefined()));

it.todo<ITagTestContext>(
'is invalid' /*, async context => {
context.invalid.map(async item =>
expect(Metadata.validate(item)).resolves.toMatchSnapshot()
);
}*/
);

test<ITagTestContext>('get latest()', context =>
context.tags.map(tagsItem => expect(tagsItem.latest).toMatchSnapshot()));

Expand Down