File size: 3,827 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { test, expect, Page } from '@playwright/test'

test.setTimeout(60 * 1000)

test.describe('code highlighting', () => {
  test.beforeEach(async ({ page }) => {
    page.goto('http://localhost:3000/examples/code-highlighting')
  })

  for (const testCase of getTestCases()) {
    const { language, content, highlights } = testCase

    test(`code highlighting ${language}`, async ({ page }) => {
      await setText(page, content, language)

      const tokens = await page
        .locator('[data-slate-editor] [data-slate-string]')
        .all()

      for (const [index, token] of tokens.entries()) {
        const highlight = highlights[index]
        const textContent = await token.textContent()

        await expect(textContent).toEqual(highlight[0])
        await expect(token).toHaveCSS('color', highlight[1])
      }
    })
  }
})

// it also tests if select and code block button works the right way
async function setText(page: Page, text: string, language: string) {
  await page.locator('[data-slate-editor]').selectText()
  await page.keyboard.press('Backspace') // clear editor
  await page.getByTestId('code-block-button').click() // convert first and the only one paragraph to code block
  await page.getByTestId('language-select').selectOption({ value: language }) // select the language option

  await page.keyboard.type(text) // type text
}

function getTestCases() {
  const testCases: {
    language: string
    content: string
    highlights: [string, string][]
  }[] = [
    {
      language: 'css',
      content: `body {
  background-color: lightblue;
}`,
      highlights: [
        ['body', 'rgb(102, 153, 0)'],
        [' ', 'rgb(0, 0, 0)'],
        ['{', 'rgb(153, 153, 153)'],
        ['  ', 'rgb(0, 0, 0)'],
        ['background-color', 'rgb(153, 0, 85)'],
        [':', 'rgb(153, 153, 153)'],
        [' lightblue', 'rgb(0, 0, 0)'],
        [';', 'rgb(153, 153, 153)'],
        ['}', 'rgb(153, 153, 153)'],
      ],
    },
    {
      language: 'html',
      content: `<body>
  <h1 class="title">Testing html</h1>
</body>`,
      highlights: [
        ['<', 'rgb(153, 0, 85)'],
        ['body', 'rgb(153, 0, 85)'],
        ['>', 'rgb(153, 0, 85)'],
        ['  ', 'rgb(0, 0, 0)'],
        ['<', 'rgb(153, 0, 85)'],
        ['h1', 'rgb(153, 0, 85)'],
        [' ', 'rgb(153, 0, 85)'],
        ['class', 'rgb(102, 153, 0)'],
        ['=', 'rgb(0, 119, 170)'],
        ['"', 'rgb(0, 119, 170)'],
        ['title', 'rgb(0, 119, 170)'],
        ['"', 'rgb(0, 119, 170)'],
        ['>', 'rgb(153, 0, 85)'],
        ['Testing html', 'rgb(0, 0, 0)'],
        ['</', 'rgb(153, 0, 85)'],
        ['h1', 'rgb(153, 0, 85)'],
        ['>', 'rgb(153, 0, 85)'],
        ['</', 'rgb(153, 0, 85)'],
        ['body', 'rgb(153, 0, 85)'],
        ['>', 'rgb(153, 0, 85)'],
      ],
    },
    {
      language: 'jsx',
      content: `<Title title="title" renderIcon={() => <Icon />} />`,
      highlights: [
        ['<', 'rgb(153, 0, 85)'],
        ['Title', 'rgb(221, 74, 104)'],
        [' ', 'rgb(153, 0, 85)'],
        ['title', 'rgb(102, 153, 0)'],
        ['=', 'rgb(0, 119, 170)'],
        ['"', 'rgb(0, 119, 170)'],
        ['title', 'rgb(0, 119, 170)'],
        ['"', 'rgb(0, 119, 170)'],
        [' ', 'rgb(153, 0, 85)'],
        ['renderIcon', 'rgb(102, 153, 0)'],
        ['=', 'rgb(153, 0, 85)'],
        ['{', 'rgb(153, 0, 85)'],
        ['(', 'rgb(153, 0, 85)'],
        [')', 'rgb(153, 0, 85)'],
        [' ', 'rgb(153, 0, 85)'],
        ['=>', 'rgb(154, 110, 58)'],
        [' ', 'rgb(153, 0, 85)'],
        ['<', 'rgb(153, 0, 85)'],
        ['Icon', 'rgb(221, 74, 104)'],
        [' ', 'rgb(153, 0, 85)'],
        ['/>', 'rgb(153, 0, 85)'],
        ['}', 'rgb(153, 0, 85)'],
        [' ', 'rgb(153, 0, 85)'],
        ['/>', 'rgb(153, 0, 85)'],
      ],
    },
  ]

  return testCases
}