Skip to content

Commit e847ee7

Browse files
committed
Attained 100% test coverage for actions module
1 parent 99fcf81 commit e847ee7

File tree

4 files changed

+79
-76
lines changed

4 files changed

+79
-76
lines changed

src/action.ts

Lines changed: 60 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,77 @@ import fs from 'fs';
55
import { ActionUtils } from './utils';
66

77
export async function action() {
8-
try {
9-
const NOTION_TOKEN_V2 = core.getInput('token_v2');
10-
const databaseId = core.getInput('database_id');
8+
const NOTION_TOKEN_V2 = core.getInput('token_v2');
9+
const databaseId = core.getInput('database_id');
1110

12-
const collectionView = await ActionUtils.fetchData<TCollectionBlock>(
13-
databaseId,
14-
'block'
15-
);
16-
core.info('Fetched database');
11+
const collectionView = await ActionUtils.fetchData<TCollectionBlock>(
12+
databaseId,
13+
'block'
14+
);
15+
core.info('Fetched database');
1716

18-
const collection_id = collectionView.collection_id;
19-
const collection = await ActionUtils.fetchData<ICollection>(
20-
collection_id,
21-
'collection'
22-
);
17+
const collection_id = collectionView.collection_id;
18+
const collection = await ActionUtils.fetchData<ICollection>(
19+
collection_id,
20+
'collection'
21+
);
2322

24-
core.info('Fetched collection');
23+
core.info('Fetched collection');
2524

26-
const { recordMap } = await NotionEndpoints.Queries.queryCollection(
27-
{
28-
collectionId: collection_id,
29-
collectionViewId: '',
30-
query: {},
31-
loader: {
32-
type: 'table',
33-
loadContentCover: false,
34-
limit: 10000,
35-
userTimeZone: ''
36-
}
37-
},
38-
{
39-
token: NOTION_TOKEN_V2,
40-
user_id: ''
25+
const { recordMap } = await NotionEndpoints.Queries.queryCollection(
26+
{
27+
collectionId: collection_id,
28+
collectionViewId: '',
29+
query: {},
30+
loader: {
31+
type: 'table',
32+
loadContentCover: false,
33+
limit: 10000,
34+
userTimeZone: ''
4135
}
42-
);
43-
44-
core.info('Fetched rows');
45-
const { schema } = collection;
46-
const [
47-
category_schema_entry,
48-
color_schema_entry
49-
] = ActionUtils.getSchemaEntries(schema);
50-
51-
const rows = ActionUtils.modifyRows(recordMap, databaseId);
36+
},
37+
{
38+
token: NOTION_TOKEN_V2,
39+
user_id: ''
40+
}
41+
);
5242

53-
if (rows.length === 0) return core.error('No database rows detected');
54-
else {
55-
const categories_map = ActionUtils.constructCategoriesMap(
56-
category_schema_entry[1]
57-
);
58-
rows.forEach((row) => {
59-
const category = row.properties[category_schema_entry[0]][0][0];
60-
if (!category) throw new Error('Each row must have a category value');
61-
const category_value = categories_map.get(category);
62-
category_value!.items.push(row.properties);
63-
});
43+
core.info('Fetched rows');
44+
const { schema } = collection;
45+
const [
46+
category_schema_entry,
47+
color_schema_entry
48+
] = ActionUtils.getSchemaEntries(schema);
6449

65-
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
66-
core.info(`Reading from ${README_PATH}`);
50+
const rows = ActionUtils.modifyRows(recordMap, databaseId);
51+
const categories_map = ActionUtils.constructCategoriesMap(
52+
category_schema_entry[1]
53+
);
54+
ActionUtils.populateCategoriesMapItems(
55+
rows,
56+
category_schema_entry[0],
57+
categories_map
58+
);
6759

68-
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
60+
const README_PATH = `${process.env.GITHUB_WORKSPACE}/README.md`;
61+
core.info(`Reading from ${README_PATH}`);
6962

70-
const [startIdx, endIdx] = ActionUtils.checkForSections(readmeLines);
71-
const newLines = ActionUtils.constructNewContents(
72-
categories_map,
73-
color_schema_entry[0]
74-
);
63+
const readmeLines = fs.readFileSync(README_PATH, 'utf-8').split('\n');
7564

76-
const finalLines = [
77-
...readmeLines.slice(0, startIdx + 1),
78-
...newLines,
79-
...readmeLines.slice(endIdx)
80-
];
65+
const [startIdx, endIdx] = ActionUtils.checkForSections(readmeLines);
66+
const newLines = ActionUtils.constructNewContents(
67+
categories_map,
68+
color_schema_entry[0]
69+
);
8170

82-
core.info(`Writing to ${README_PATH}`);
71+
const finalLines = [
72+
...readmeLines.slice(0, startIdx + 1),
73+
...newLines,
74+
...readmeLines.slice(endIdx)
75+
];
8376

84-
fs.writeFileSync(README_PATH, finalLines.join('\n'), 'utf-8');
77+
core.info(`Writing to ${README_PATH}`);
8578

86-
try {
87-
await ActionUtils.commitFile();
88-
} catch (err) {
89-
core.setFailed(err.message);
90-
}
91-
}
92-
} catch (error) {
93-
core.setFailed(error.message);
94-
}
79+
fs.writeFileSync(README_PATH, finalLines.join('\n'), 'utf-8');
80+
await ActionUtils.commitFile();
9581
}

src/utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { constructNewContents } from './constructNewContents';
55
import { fetchData } from './fetchData';
66
import { getSchemaEntries } from './getSchemaEntries';
77
import { modifyRows } from './modifyRows';
8+
import { populateCategoriesMapItems } from './populateCategoriesMapItems';
89

910
export const ActionUtils = {
1011
checkForSections,
@@ -13,5 +14,6 @@ export const ActionUtils = {
1314
constructNewContents,
1415
fetchData,
1516
getSchemaEntries,
16-
modifyRows
17+
modifyRows,
18+
populateCategoriesMapItems
1719
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { IPage } from '@nishans/types';
2+
import { ICategoryMap } from '../types';
3+
4+
export const populateCategoriesMapItems = (
5+
rows: IPage[],
6+
category_schema_id: string,
7+
categories_map: ICategoryMap
8+
) => {
9+
rows.forEach((row) => {
10+
const category = row.properties[category_schema_id][0][0];
11+
if (!category) throw new Error('Each row must have a category value');
12+
const category_value = categories_map.get(category);
13+
category_value!.items.push(row.properties);
14+
});
15+
};

tests/action.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ it(`Should work`, async () => {
134134
.spyOn(fs, 'readFileSync')
135135
.mockImplementationOnce(
136136
() =>
137-
'# Header\n\nfirst\n<!--START_SECTION:learn-->\n<!--END_SECTION:learn-->\nsecond'
137+
'# Header\nfirst\n<!--START_SECTION:learn-->\n<!--END_SECTION:learn-->\nsecond'
138138
);
139139
jest
140140
.spyOn(ActionUtils, 'checkForSections')

0 commit comments

Comments
 (0)