/
postAction.test.ts
83 lines (66 loc) · 1.96 KB
/
postAction.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as cache from '../../src/cache'
import {postAction} from '../../src/actions'
jest.mock('../../src/cache', () => {
return {
cache: jest.fn(),
saveFactory: jest.fn().mockReturnValue(jest.fn())
}
})
jest.mock('../../src/utils/getCacheKeys', () => {
return {
getCacheKeys: jest.fn().mockResolvedValue({
key: 'cache-key-mock',
restoreKeys: ['cache-key-', 'cache-']
})
}
})
jest.mock('../../src/utils/getComposerCacheDir', () => {
return {
getComposerCacheDir: jest.fn().mockResolvedValue('/path/to/composer/cache')
}
})
describe('post action', () => {
const OLD_ENV = process.env
beforeEach(() => {
jest.resetModules()
process.env = {...OLD_ENV}
})
afterAll(() => {
process.env = OLD_ENV
})
test('runs', async () => {
const cacheMock = jest.spyOn(cache, 'cache')
const mockFactory = cache.saveFactory()
process.env['INPUT_COMPOSER-OPTIONS'] = ''
process.env['INPUT_DEPENDENCY-VERSIONS'] = 'highest'
await postAction()
expect(cacheMock).toHaveBeenCalledTimes(1)
expect(cacheMock).toHaveBeenCalledWith(
mockFactory,
['/path/to/composer/cache'],
'cache-key-mock',
['cache-key-', 'cache-']
)
})
test('runs with a custom working-directory', async () => {
const cacheMock = jest.spyOn(cache, 'cache')
const mockFactory = cache.saveFactory()
process.env['INPUT_COMPOSER-OPTIONS'] = ' --working-dir subdirectory'
process.env['INPUT_DEPENDENCY-VERSIONS'] = 'highest'
await postAction()
expect(cacheMock).toHaveBeenCalledTimes(1)
expect(cacheMock).toHaveBeenCalledWith(
mockFactory,
['/path/to/composer/cache'],
'cache-key-mock',
['cache-key-', 'cache-']
)
})
test('sets failure state with error', async () => {
const cacheMock = jest.spyOn(cache, 'cache').mockRejectedValue({
message: 'a mocked error message'
})
await postAction()
expect(cacheMock).toHaveBeenCalledTimes(1)
})
})