|
1 | 1 | const searchAndReplaceSpecialAnnotations = require('../../../../lib/actions/lib/searchAndReplaceSpecialAnnotation')
|
2 | 2 |
|
3 | 3 | describe('searchAndReplaceSpecialAnnotations', () => {
|
4 |
| - test('does not affect input if no special annotations are found', () => { |
5 |
| - const payload = { |
6 |
| - user: { |
7 |
| - login: 'creator' |
8 |
| - } |
9 |
| - } |
10 |
| - expect(searchAndReplaceSpecialAnnotations('no special annotations', payload)).toBe('no special annotations') |
11 |
| - }) |
12 |
| - |
13 |
| - test('special annotation at the beginning of string works properly', () => { |
14 |
| - const payload = { |
15 |
| - user: { |
16 |
| - login: 'creator' |
17 |
| - } |
18 |
| - } |
19 |
| - expect(searchAndReplaceSpecialAnnotations('@author says hello!', payload)).toBe('creator says hello!') |
20 |
| - }) |
21 |
| - |
22 |
| - test('escape character works properly', () => { |
23 |
| - const payload = { |
24 |
| - user: { |
25 |
| - login: 'creator' |
26 |
| - } |
| 4 | + const SPECIAL_ANNOTATION = { |
| 5 | + '@author': 'creator', |
| 6 | + '@action': 'created', |
| 7 | + '@sender': 'initiator', |
| 8 | + '@bot': 'Mergeable[bot]', |
| 9 | + '@repository': 'botrepo' |
| 10 | + } |
| 11 | + const tests = [ |
| 12 | + { |
| 13 | + name: 'does not affect input if no special annotations are found', |
| 14 | + message: 'no special annotations', |
| 15 | + expected: 'no special annotations' |
| 16 | + }, |
| 17 | + { |
| 18 | + name: 'special annotation at the beginning of string works properly', |
| 19 | + message: '$annotation$ says hello!', |
| 20 | + expected: '$annotation$ says hello!' |
| 21 | + }, |
| 22 | + { |
| 23 | + name: 'escape character works properly', |
| 24 | + message: 'this is \\@author', |
| 25 | + expected: 'this is @author' |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'special annotation at the end of string works properly', |
| 29 | + message: 'this is $annotation$', |
| 30 | + expected: 'this is $annotation$' |
| 31 | + }, |
| 32 | + { |
| 33 | + name: '@@annotation is replaced, prepending @ remains', |
| 34 | + message: 'this is @$annotation$', |
| 35 | + expected: 'this is @$annotation$' |
| 36 | + }, |
| 37 | + { |
| 38 | + name: 'replaces special annotation anywhere in the text', |
| 39 | + message: 'this is something$annotation$ speaking', |
| 40 | + expected: 'this is something$annotation$ speaking' |
27 | 41 | }
|
28 |
| - expect(searchAndReplaceSpecialAnnotations('this is \\@author', payload)).toBe('this is @author') |
29 |
| - }) |
| 42 | + ] |
30 | 43 |
|
31 |
| - test('@author is replaced by payload.user.login', () => { |
32 |
| - const payload = { |
33 |
| - user: { |
34 |
| - login: 'creator' |
| 44 | + test.each(tests)( |
| 45 | + '$name', |
| 46 | + async ({ message, expected }) => { |
| 47 | + const payload = { |
| 48 | + user: { |
| 49 | + login: 'creator' |
| 50 | + } |
35 | 51 | }
|
36 |
| - } |
37 |
| - expect(searchAndReplaceSpecialAnnotations('this is @author', payload)).toBe('this is creator') |
38 |
| - }) |
39 |
| - |
40 |
| - test('@@author is replaced by @payload.user.login', () => { |
41 |
| - const payload = { |
42 |
| - user: { |
43 |
| - login: 'creator' |
| 52 | + const evt = { |
| 53 | + action: 'created', |
| 54 | + repository: { |
| 55 | + full_name: 'botrepo' |
| 56 | + }, |
| 57 | + sender: { |
| 58 | + login: 'initiator' |
| 59 | + } |
44 | 60 | }
|
45 |
| - } |
46 |
| - expect(searchAndReplaceSpecialAnnotations('this is @@author', payload)).toBe('this is @creator') |
47 |
| - }) |
48 | 61 |
|
49 |
| - test('replaces annotation anywhere in the text', () => { |
50 |
| - const payload = { |
51 |
| - user: { |
52 |
| - login: 'creator' |
| 62 | + for (const annotation of Object.keys(SPECIAL_ANNOTATION)) { |
| 63 | + const messageWithAnnotation = message.replace('$annotation$', annotation) |
| 64 | + const messageWithReplacement = expected.replace('$annotation$', SPECIAL_ANNOTATION[annotation]) |
| 65 | + expect(searchAndReplaceSpecialAnnotations(messageWithAnnotation, payload, evt)).toBe(messageWithReplacement) |
53 | 66 | }
|
54 | 67 | }
|
55 |
| - expect(searchAndReplaceSpecialAnnotations('this is something@author speaking', payload)).toBe('this is somethingcreator speaking') |
56 |
| - }) |
| 68 | + ) |
57 | 69 | })
|
0 commit comments