|
1 | 1 | import{parse,Rule}from'postcss'; |
| 2 | +import{DEFAULT_SEPARATORS}from'#modules/bem/constants'; |
2 | 3 | import{getBemBlock}from'./get-bem-block'; |
3 | 4 |
|
4 | 5 | describe(getBemBlock,()=>{ |
5 | | -it('Returns null when no rules present',()=>{ |
6 | | -constroot=parse(``); |
| 6 | +describe('Negative scenarios',()=>{ |
| 7 | +it('Returns `null` when no rules present',()=>{ |
| 8 | +constroot=parse(``); |
7 | 9 |
|
8 | | -expect(getBemBlock(root)).toBeNull(); |
9 | | -}); |
10 | | - |
11 | | -it('Returns null when no class selectors present',()=>{ |
12 | | -constroot=parse(`body { color: red; }`); |
13 | | - |
14 | | -expect(getBemBlock(root)).toBeNull(); |
15 | | -}); |
| 10 | +expect(getBemBlock(root,DEFAULT_SEPARATORS)).toBeNull(); |
| 11 | +}); |
16 | 12 |
|
17 | | -it('Returns null forruleswithmultiple selectors',()=>{ |
18 | | -constroot=parse(`.a, .b { color: red; }`); |
| 13 | +it('Returns`null` forclass selectorwithonly "."',()=>{ |
| 14 | +constroot=parse(`. { color: red; }`); |
19 | 15 |
|
20 | | -expect(getBemBlock(root)).toBeNull(); |
21 | | -}); |
22 | | - |
23 | | -it('Returns null for class selector with only "."',()=>{ |
24 | | -constroot=parse(`. { color: red; }`); |
25 | | - |
26 | | -expect(getBemBlock(root)).toBeNull(); |
27 | | -}); |
| 16 | +expect(getBemBlock(root,DEFAULT_SEPARATORS)).toBeNull(); |
| 17 | +}); |
28 | 18 |
|
29 | | -it('Returns correct block for simple class selector',()=>{ |
30 | | -constroot=parse(`.block { color: red; }`); |
31 | | -constresult=getBemBlock(root); |
| 19 | +it('Returns `null` when no class selectors present',()=>{ |
| 20 | +constroot=parse(`body { color: red; }`); |
32 | 21 |
|
33 | | -expect(result?.blockName).toBe('block'); |
34 | | -expect(result?.selector).toBe('.block'); |
35 | | -expect(result?.rule).toBeInstanceOf(Rule); |
36 | | -}); |
| 22 | +expect(getBemBlock(root,DEFAULT_SEPARATORS)).toBeNull(); |
| 23 | +}); |
37 | 24 |
|
38 | | -it('Returns correct block for selector like "html .block"',()=>{ |
39 | | -constroot=parse(`html .block { color: red; }`); |
40 | | -constresult=getBemBlock(root); |
| 25 | +it('Returns `null` for rules that contain more than just classes',()=>{ |
| 26 | +constroot=parse(`#foo, .foo-component { color: red; }`); |
41 | 27 |
|
42 | | -expect(result?.blockName).toBe('block'); |
43 | | -expect(result?.selector).toBe('.block'); |
44 | | -expect(result?.rule).toBeInstanceOf(Rule); |
45 | | -}); |
| 28 | +expect(getBemBlock(root,DEFAULT_SEPARATORS)).toBeNull(); |
| 29 | +}); |
46 | 30 |
|
47 | | -it('Skips rules inside disallowed at-rules',()=>{ |
48 | | -constroot=parse(` |
49 | | -@supports (display: grid) { |
50 | | -.block { color: red; } |
51 | | -} |
52 | | -`); |
| 31 | +it('Returns `null` for multiple classes if they belong to different bem blocks',()=>{ |
| 32 | +constroot=parse(`.a, .b { color: red; }`); |
53 | 33 |
|
54 | | -expect(getBemBlock(root)).toBeNull(); |
55 | | -}); |
| 34 | +expect(getBemBlock(root,DEFAULT_SEPARATORS)).toBeNull(); |
| 35 | +}); |
56 | 36 |
|
57 | | -it('Allows rules inside @layer',()=>{ |
58 | | -constroot=parse(` |
59 | | -@layer components { |
60 | | -.block { color: red; } |
61 | | -} |
62 | | -`); |
63 | | -constresult=getBemBlock(root); |
| 37 | +it('Skips rules inside disallowed at-rules',()=>{ |
| 38 | +constroot=parse(` |
| 39 | +@supports (display: grid) { |
| 40 | +.block { color: red; } |
| 41 | +} |
| 42 | +`); |
64 | 43 |
|
65 | | -expect(result?.blockName).toBe('block'); |
66 | | -expect(result?.selector).toBe('.block'); |
67 | | -expect(result?.rule).toBeInstanceOf(Rule); |
| 44 | +expect(getBemBlock(root,DEFAULT_SEPARATORS)).toBeNull(); |
| 45 | +}); |
68 | 46 | }); |
69 | 47 |
|
70 | | -it('Allows rules inside @media',()=>{ |
71 | | -constroot=parse(` |
72 | | -@media (width >= 768px) { |
| 48 | +describe('Positive scenarios',()=>{ |
| 49 | +it('Returns correct block for simple class selector',()=>{ |
| 50 | +constroot=parse(`.block { color: red; }`); |
| 51 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
| 52 | + |
| 53 | +expect(result?.blockName).toBe('block'); |
| 54 | +expect(result?.selector).toBe('.block'); |
| 55 | +expect(result?.rule).toBeInstanceOf(Rule); |
| 56 | +}); |
| 57 | + |
| 58 | +it('Returns correct block for a BEM element',()=>{ |
| 59 | +constroot=parse(`.block__element { color: red; }`); |
| 60 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
| 61 | + |
| 62 | +expect(result?.blockName).toBe('block'); |
| 63 | +expect(result?.selector).toBe('.block'); |
| 64 | +expect(result?.rule).toBeInstanceOf(Rule); |
| 65 | +}); |
| 66 | + |
| 67 | +it('Returns correct block for multiple selectors of the same block',()=>{ |
| 68 | +constroot=parse(`.block--modifier, .block { color: red; }`); |
| 69 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
| 70 | + |
| 71 | +expect(result?.blockName).toBe('block'); |
| 72 | +expect(result?.selector).toBe('.block'); |
| 73 | +expect(result?.rule).toBeInstanceOf(Rule); |
| 74 | +}); |
| 75 | + |
| 76 | +it('Returns correct block for a selector like "html .block"',()=>{ |
| 77 | +constroot=parse(`html .block { color: red; }`); |
| 78 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
| 79 | + |
| 80 | +expect(result?.blockName).toBe('block'); |
| 81 | +expect(result?.selector).toBe('.block'); |
| 82 | +expect(result?.rule).toBeInstanceOf(Rule); |
| 83 | +}); |
| 84 | + |
| 85 | +it('Allows rules inside `@layer`',()=>{ |
| 86 | +constroot=parse(` |
| 87 | +@layer components { |
| 88 | +.block { color: red; } |
| 89 | +} |
| 90 | +`); |
| 91 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
| 92 | + |
| 93 | +expect(result?.blockName).toBe('block'); |
| 94 | +expect(result?.selector).toBe('.block'); |
| 95 | +expect(result?.rule).toBeInstanceOf(Rule); |
| 96 | +}); |
| 97 | + |
| 98 | +it('Allows rules inside `@media`',()=>{ |
| 99 | +constroot=parse(` |
| 100 | +@media (width >= 768px) { |
| 101 | +.block { color: red; } |
| 102 | +} |
| 103 | +`); |
| 104 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
| 105 | + |
| 106 | +expect(result?.blockName).toBe('block'); |
| 107 | +expect(result?.selector).toBe('.block'); |
| 108 | +expect(result?.rule).toBeInstanceOf(Rule); |
| 109 | +}); |
| 110 | + |
| 111 | +it('Returns first valid block and ignores others',()=>{ |
| 112 | +constroot=parse(` |
73 | 113 | .block { color: red; } |
74 | | -} |
75 | | -`); |
76 | | -constresult=getBemBlock(root); |
77 | | - |
78 | | -expect(result?.blockName).toBe('block'); |
79 | | -expect(result?.selector).toBe('.block'); |
80 | | -expect(result?.rule).toBeInstanceOf(Rule); |
81 | | -}); |
82 | | - |
83 | | -it('Returns first valid block and ignores others',()=>{ |
84 | | -constroot=parse(` |
85 | | -.block { color: red; } |
86 | | -.another { color: blue; } |
87 | | -`); |
88 | | -constresult=getBemBlock(root); |
| 114 | +.another { color: blue; } |
| 115 | +`); |
| 116 | +constresult=getBemBlock(root,DEFAULT_SEPARATORS); |
89 | 117 |
|
90 | | -expect(result?.blockName).toBe('block'); |
| 118 | +expect(result?.blockName).toBe('block'); |
| 119 | +}); |
91 | 120 | }); |
92 | 121 | }); |