This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
format-comment.test.js
90 lines (63 loc) · 3.08 KB
/
format-comment.test.js
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
84
85
86
87
88
89
90
const formatComment = require('./format-comment.js');
describe('languages', () => {
test('Formats a JavaScript code block', () => {
const comment = '```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
test('Formats a CSS code block', () => {
const comment = '```css\n.foo {color: red;}\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
test('Formats a TypeScript code block', () => {
const comment = '```ts\ninterface ITypeScript {supported: boolean}\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
test('Formats a JSON code block', () => {
const comment = '```json\n{"supportJSON": true}\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
test('Formats a GraphQL code block', () => {
const comment = '```graphql\n{language(name: "GraphQL") {isSupported}}\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
});
test('Formats multiple code blocks', () => {
const comment = '```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```\n```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
test('Overrides notice if the comment has been formatted previously', () => {
const comment = '<!-- The following code block was formatted with Prettier. If this is not desired, please change this comment to `prettier-github disable`. A copy of your original code block is included below in case you want to restore it.\nLearn more about Prettier GitHub` at https://github.com/jgierer12/prettier-github\n\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n\n-->\n```js\nbar()\n```';
expect(formatComment(comment)).toMatchSnapshot();
});
test('Ignores no code blocks', () => {
const comment = 'Nothing to format here\n';
expect(formatComment(comment)).toBe(comment);
});
test('Ignores incompatible code blocks', () => {
const comment = '```sh\necho "Nothing to format here"\n```';
expect(formatComment(comment)).toBe(comment);
});
test('Ignores correctly formatted code blocks', () => {
const comment = '```js\nconst all = "good";\n```';
expect(formatComment(comment)).toBe(comment);
});
test('Ignores comments containing `<!-- prettier-github disable -->`', () => {
const comment = '<!-- prettier-github disable -->\n\n```js\nfoo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());\n```';
expect(formatComment(comment)).toBe(comment);
});
describe('invalid code blocks', () => {
beforeEach(() => {
global.console = {
warn: jest.fn()
};
});
test('Warns about invalid code blocks', () => {
const comment = '```js\ninvalid javascript\n```';
formatComment(comment);
expect(global.console.warn).toHaveBeenCalled();
});
test('Ignores invalid code blocks', () => {
const comment = '```js\ninvalid javascript\n```';
expect(formatComment(comment)).toBe(comment);
});
});