Skip to content

Commit

Permalink
Fix isSharedLineComment.test.js that uses callbacks (#4958)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Oct 2, 2020
1 parent 0894cb4 commit a5e4355
Showing 1 changed file with 62 additions and 70 deletions.
132 changes: 62 additions & 70 deletions lib/utils/__tests__/isSharedLineComment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@ const postcss = require('postcss');

describe('isSharedLineComment', () => {
it('returns false for the first node', () => {
const root = postcss.parse(`
const css = `
/* comment */
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(false);
expect(isSharedLineComment(comment(css))).toBe(false);
});

it('returns false for a non-shared-line comment before a rule', () => {
const root = postcss.parse(`
const css = `
/* comment */
a {}
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(false);
expect(isSharedLineComment(comment(css))).toBe(false);
});

it('returns false for a non-shared-line comment after a rule', () => {
const root = postcss.parse(`
const css = `
a {}
/* comment */
`);
`;

expect(isSharedLineComment(root.nodes[1])).toBe(false);
expect(isSharedLineComment(comment(css))).toBe(false);
});

it('returns true for a shared-line comment before a rule', () => {
const root = postcss.parse(`
const css = `
/* comment */ a {}
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(true);
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true for a shared-line comment after a rule', () => {
const root = postcss.parse(`
const css = `
a {} /* comment */
`);
`;

expect(isSharedLineComment(root.nodes[1])).toBe(true);
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns false for a shared-line non-comment', () => {
Expand All @@ -56,152 +56,144 @@ describe('isSharedLineComment', () => {
});

it('returns true when comment shares a line with the start of a rule block, before it', () => {
const root = postcss.parse(`
const css = `
/* comment */ a {
color: pink;
}
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(true);
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of a rule block with a multiline selector, before it', () => {
const root = postcss.parse(`
const css = `
/* comment */ a,
b {
color: pink;
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of a rule block, after it', () => {
const root = postcss.parse(`
const css = `
a { /* comment */
color: pink;
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of a rule block with a multiline selector, after it', () => {
const root = postcss.parse(`
const css = `
a,
b { /* comment */
color: pink;
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of an at-rule block, before it', () => {
const root = postcss.parse(`
const css = `
/* comment */ @media (min-width: 0px) {
a { color: pink; }
}
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(true);
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of an at-rule block with a multiline selector, before it', () => {
const root = postcss.parse(`
const css = `
/* comment */ @media
(min-width: 0px) {
a { color: pink; }
}
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(true);
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of an at-rule block, after it', () => {
const root = postcss.parse(`
const css = `
@media (min-width: 0px) { /* comment */
a { color: pink; }
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of an at-rule block with a multiline selector, after it', () => {
const root = postcss.parse(`
const css = `
@media
(min-width: 0px) { /* comment */
a { color: pink; }
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns false when comment shares a line with only another comment', () => {
const root = postcss.parse(`
const css = `
/* comment */ /* comment */
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(false);
expect(isSharedLineComment(comment(css))).toBe(false);
});

it('returns true when comment shares a line with another comment and a non-comment', () => {
const root = postcss.parse(`
const css = `
/* comment */ /* comment */ a {}
`);
`;

expect(isSharedLineComment(root.nodes[0])).toBe(true);
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the end of a multi-line rule block, after it', () => {
const root = postcss.parse(`
const css = `
a {
color: pink;
} /* comment */
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the end of a multi-line property declaration, after it', () => {
const root = postcss.parse(`
const css = `
a {
border-radius:
1em
0; /* comment */
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});

it('returns true when comment shares a line with the start of a multi-line property declaration, before it', () => {
const root = postcss.parse(`
const css = `
a {
/* comment */ border-radius:
1em
0;
}
`);
`;

root.walkComments((comment) => {
expect(isSharedLineComment(comment)).toBe(true);
});
expect(isSharedLineComment(comment(css))).toBe(true);
});
});

function comment(css) {
const comments = [];

postcss.parse(css).walkComments((comment) => comments.push(comment));

return comments[0];
}

0 comments on commit a5e4355

Please sign in to comment.