Skip to content

Commit e48ab3d

Browse files
committed
add autoUpdateReadme
1 parent 4df0f7c commit e48ab3d

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/tables/autoUpdateReadme.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { statsAction } from '../commands/stats.js';
2+
import { getConfigValue } from '../config.js';
3+
import { logger } from '../logger.js';
4+
5+
/**
6+
* If readme auto updates are enabled, will update the progress table of the readme
7+
*/
8+
export const autoUpdateReadme = async () => {
9+
if (getConfigValue('disableReadmeAutoSaveProgress')) {
10+
logger.verbose('not updating readme file, auto update disabled');
11+
return;
12+
}
13+
await statsAction({ save: true });
14+
};

tests/tables/autoUpdateReadme.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, jest, test, afterEach } from '@jest/globals';
2+
import { mockLogger, easyMock, easyResolve, mockConfig } from '../mocks.js';
3+
4+
// setup mocks
5+
const easyMocks = [['src/commands/stats.js', ['statsAction']]];
6+
easyMock(easyMocks);
7+
mockLogger();
8+
const { getConfigValue } = mockConfig();
9+
10+
// import after mocks set up.
11+
const { statsAction } = await easyResolve(easyMocks);
12+
const { autoUpdateReadme } = await import(
13+
'../../src/tables/autoUpdateReadme.js'
14+
);
15+
16+
describe('autoUpdateReadme()', () => {
17+
afterEach(() => {
18+
jest.resetAllMocks();
19+
});
20+
21+
test('does not update readme if not configured to do so', async () => {
22+
getConfigValue.mockImplementation((key) => {
23+
if (key === 'disableReadmeAutoSaveProgress') {
24+
return true;
25+
}
26+
throw new Error('unknown key');
27+
});
28+
await autoUpdateReadme();
29+
expect(statsAction).not.toHaveBeenCalled();
30+
});
31+
32+
test('update readme if configured to do so', async () => {
33+
getConfigValue.mockImplementation((key) => {
34+
if (key === 'disableReadmeAutoSaveProgress') {
35+
return false;
36+
}
37+
throw new Error('unknown key');
38+
});
39+
await autoUpdateReadme();
40+
expect(statsAction).toHaveBeenCalled();
41+
});
42+
43+
test('passes save option to statsAction', async () => {
44+
getConfigValue.mockImplementation((key) => {
45+
if (key === 'disableReadmeAutoSaveProgress') {
46+
return false;
47+
}
48+
throw new Error('unknown key');
49+
});
50+
await autoUpdateReadme();
51+
expect(statsAction).toHaveBeenCalledWith({ save: true });
52+
});
53+
});

0 commit comments

Comments
 (0)