|
1 | 1 | const CustomProperties = require('../../../../lib/plugins/custom_properties')
|
2 | 2 |
|
3 | 3 | describe('CustomProperties', () => {
|
| 4 | + const nop = false |
4 | 5 | let github
|
5 | 6 | let log
|
6 | 7 |
|
| 8 | + const owner = 'test-owner' |
| 9 | + const repo = 'test-repo' |
| 10 | + |
7 | 11 | function configure (config) {
|
8 |
| - const nop = false |
9 |
| - const errors = [] |
10 |
| - return new CustomProperties(nop, github, { owner: 'bkeepers', repo: 'test' }, config, log, errors) |
| 12 | + return new CustomProperties(nop, github, { owner, repo }, config, log, []) |
11 | 13 | }
|
12 | 14 |
|
13 | 15 | beforeEach(() => {
|
14 | 16 | github = {
|
15 |
| - request: jest.fn() |
16 |
| - // .mockResolvedValue({ |
17 |
| - // data: [ |
18 |
| - // { property_name: 'test', value: 'test' } |
19 |
| - // ] |
20 |
| - // }) |
| 17 | + paginate: jest.fn(), |
| 18 | + repos: { |
| 19 | + getCustomPropertiesValues: jest.fn(), |
| 20 | + createOrUpdateCustomPropertiesValues: jest.fn() |
| 21 | + } |
21 | 22 | }
|
| 23 | + |
22 | 24 | log = { debug: jest.fn(), error: console.error }
|
23 | 25 | })
|
24 | 26 |
|
25 |
| - describe('sync', () => { |
26 |
| - it('syncs custom properties', async () => { |
27 |
| - const plugin = configure([ |
28 |
| - { name: 'test', value: 'test' } |
29 |
| - ]) |
| 27 | + describe('Custom Properties plugin', () => { |
| 28 | + it('should normalize entries when be instantiated', () => { |
| 29 | + const plugin = configure([{ name: 'Test', value: 'test' }]) |
| 30 | + expect(plugin.entries).toEqual([{ name: 'test', value: 'test' }]) |
| 31 | + }) |
30 | 32 |
|
31 |
| - github.request.mockResolvedValue({ |
32 |
| - data: [ |
33 |
| - { property_name: 'test', value: 'test' } |
34 |
| - ] |
35 |
| - }) |
| 33 | + it('should fetch and normalize custom properties successfully', async () => { |
| 34 | + const mockResponse = [ |
| 35 | + { property_name: 'Test1', value: 'value1' }, |
| 36 | + { property_name: 'Test2', value: 'value2' } |
| 37 | + ] |
36 | 38 |
|
37 |
| - return plugin.sync().then(() => { |
38 |
| - expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/properties/values', { |
39 |
| - org: 'bkeepers', |
40 |
| - repo: 'test' |
41 |
| - }) |
42 |
| - }) |
| 39 | + github.paginate.mockResolvedValue(mockResponse) |
| 40 | + |
| 41 | + const plugin = configure() |
| 42 | + const result = await plugin.find() |
| 43 | + |
| 44 | + expect(github.paginate).toHaveBeenCalledWith( |
| 45 | + github.repos.getCustomPropertiesValues, |
| 46 | + { |
| 47 | + owner, |
| 48 | + repo, |
| 49 | + per_page: 100 |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + expect(result).toEqual([ |
| 54 | + { name: 'test1', value: 'value1' }, |
| 55 | + { name: 'test2', value: 'value2' } |
| 56 | + ]) |
43 | 57 | })
|
44 |
| - }) |
45 |
| - describe('sync', () => { |
46 |
| - it('add custom properties', async () => { |
| 58 | + |
| 59 | + it('should sync', async () => { |
| 60 | + const mockResponse = [ |
| 61 | + { property_name: 'no-change', value: 'no-change' }, |
| 62 | + { property_name: 'new-value', value: '' }, |
| 63 | + { property_name: 'update-value', value: 'update-value' }, |
| 64 | + { property_name: 'delete-value', value: 'update-value' } |
| 65 | + ] |
| 66 | + |
| 67 | + github.paginate.mockResolvedValue(mockResponse) |
| 68 | + |
47 | 69 | const plugin = configure([
|
48 |
| - { name: 'test', value: 'test' } |
| 70 | + { name: 'no-change', value: 'no-change' }, |
| 71 | + { name: 'new-value', value: 'new-value' }, |
| 72 | + { name: 'update-value', value: 'new-value' }, |
| 73 | + { name: 'delete-value', value: null } |
49 | 74 | ])
|
50 | 75 |
|
51 |
| - github.request.mockResolvedValue({ |
52 |
| - data: [] |
53 |
| - }) |
54 |
| - |
55 | 76 | return plugin.sync().then(() => {
|
56 |
| - expect(github.request).toHaveBeenNthCalledWith(1, 'GET /repos/:org/:repo/properties/values', { |
57 |
| - org: 'bkeepers', |
58 |
| - repo: 'test' |
59 |
| - }) |
60 |
| - expect(github.request).toHaveBeenNthCalledWith(2, 'PATCH /repos/:org/:repo/properties/values', { |
61 |
| - org: 'bkeepers', |
62 |
| - repo: 'test', |
| 77 | + expect(github.paginate).toHaveBeenCalledWith( |
| 78 | + github.repos.getCustomPropertiesValues, |
| 79 | + { |
| 80 | + owner, |
| 81 | + repo, |
| 82 | + per_page: 100 |
| 83 | + } |
| 84 | + ) |
| 85 | + expect(github.repos.createOrUpdateCustomPropertiesValues).not.toHaveBeenCalledWith({ |
| 86 | + owner, |
| 87 | + repo, |
63 | 88 | properties: [
|
64 | 89 | {
|
65 |
| - property_name: 'test', |
66 |
| - value: 'test' |
| 90 | + property_name: 'no-change', |
| 91 | + value: 'no-change' |
67 | 92 | }
|
68 | 93 | ]
|
69 | 94 | })
|
70 |
| - }) |
71 |
| - }) |
72 |
| - }) |
73 |
| - describe('sync', () => { |
74 |
| - it('remove custom properties', async () => { |
75 |
| - const plugin = configure([]) |
76 |
| - |
77 |
| - github.request.mockResolvedValue({ |
78 |
| - data: [{ property_name: 'test', value: 'test' }] |
79 |
| - }) |
80 |
| - |
81 |
| - return plugin.sync().then(() => { |
82 |
| - expect(github.request).toHaveBeenNthCalledWith(1, 'GET /repos/:org/:repo/properties/values', { |
83 |
| - org: 'bkeepers', |
84 |
| - repo: 'test' |
85 |
| - }) |
86 |
| - expect(github.request).toHaveBeenNthCalledWith(2, 'PATCH /repos/:org/:repo/properties/values', { |
87 |
| - org: 'bkeepers', |
88 |
| - repo: 'test', |
| 95 | + expect(github.repos.createOrUpdateCustomPropertiesValues).toHaveBeenCalledWith({ |
| 96 | + owner, |
| 97 | + repo, |
89 | 98 | properties: [
|
90 | 99 | {
|
91 |
| - property_name: 'test', |
92 |
| - value: null |
| 100 | + property_name: 'new-value', |
| 101 | + value: 'new-value' |
93 | 102 | }
|
94 | 103 | ]
|
95 | 104 | })
|
96 |
| - }) |
97 |
| - }) |
98 |
| - }) |
99 |
| - describe('sync', () => { |
100 |
| - it('update custom properties', async () => { |
101 |
| - const plugin = configure([ |
102 |
| - { name: 'test', value: 'foobar' } |
103 |
| - ]) |
104 |
| - |
105 |
| - github.request.mockResolvedValue({ |
106 |
| - data: [{ property_name: 'test', value: 'test' }] |
107 |
| - }) |
108 |
| - |
109 |
| - return plugin.sync().then(() => { |
110 |
| - expect(github.request).toHaveBeenNthCalledWith(1, 'GET /repos/:org/:repo/properties/values', { |
111 |
| - org: 'bkeepers', |
112 |
| - repo: 'test' |
| 105 | + expect(github.repos.createOrUpdateCustomPropertiesValues).toHaveBeenCalledWith({ |
| 106 | + owner, |
| 107 | + repo, |
| 108 | + properties: [ |
| 109 | + { |
| 110 | + property_name: 'update-value', |
| 111 | + value: 'new-value' |
| 112 | + } |
| 113 | + ] |
113 | 114 | })
|
114 |
| - expect(github.request).toHaveBeenNthCalledWith(2, 'PATCH /repos/:org/:repo/properties/values', { |
115 |
| - org: 'bkeepers', |
116 |
| - repo: 'test', |
| 115 | + expect(github.repos.createOrUpdateCustomPropertiesValues).toHaveBeenCalledWith({ |
| 116 | + owner, |
| 117 | + repo, |
117 | 118 | properties: [
|
118 | 119 | {
|
119 |
| - property_name: 'test', |
120 |
| - value: 'foobar' |
| 120 | + property_name: 'delete-value', |
| 121 | + value: null |
121 | 122 | }
|
122 | 123 | ]
|
123 | 124 | })
|
124 | 125 | })
|
| 126 | + |
| 127 | + // const plugin = configure([{ name: 'Test', value: 'test' }]) |
| 128 | + // await plugin.update({ name: 'test', value: 'old' }, { name: 'test', value: 'test' }) |
| 129 | + |
| 130 | + // expect(github.repos.createOrUpdateCustomPropertiesValues).toHaveBeenCalledWith({ |
| 131 | + // owner, |
| 132 | + // repo, |
| 133 | + // properties: [ |
| 134 | + // { |
| 135 | + // property_name: 'test', |
| 136 | + // value: 'test' |
| 137 | + // } |
| 138 | + // ] |
| 139 | + // }) |
125 | 140 | })
|
126 | 141 | })
|
127 | 142 | })
|
0 commit comments