|
| 1 | +import { expect } from 'vitest' |
| 2 | +import { defineTest, js } from '../../src/testing' |
| 3 | +import { createClient } from '../utils/client' |
| 4 | +import * as fs from 'node:fs/promises' |
| 5 | + |
| 6 | +defineTest({ |
| 7 | + name: 'Changing the separator registers new trigger characters', |
| 8 | + fs: { |
| 9 | + 'tailwind.config.js': js` |
| 10 | + module.exports = { |
| 11 | + separator: ':', |
| 12 | + } |
| 13 | + `, |
| 14 | + }, |
| 15 | + prepare: async ({ root }) => ({ client: await createClient({ root }) }), |
| 16 | + handle: async ({ root, client }) => { |
| 17 | + // Initially don't have any registered capabilities because dynamic |
| 18 | + // registration is delayed until after project initialization |
| 19 | + expect(client.serverCapabilities).toEqual([]) |
| 20 | + |
| 21 | + // We open a document so a project gets initialized |
| 22 | + await client.open({ |
| 23 | + lang: 'html', |
| 24 | + text: '<div class="bg-[#000]/25 hover:">', |
| 25 | + }) |
| 26 | + |
| 27 | + // And now capabilities are registered |
| 28 | + expect(client.serverCapabilities).toEqual( |
| 29 | + expect.arrayContaining([ |
| 30 | + expect.objectContaining({ |
| 31 | + method: 'textDocument/hover', |
| 32 | + }), |
| 33 | + |
| 34 | + expect.objectContaining({ |
| 35 | + method: 'textDocument/completion', |
| 36 | + registerOptions: { |
| 37 | + documentSelector: null, |
| 38 | + resolveProvider: true, |
| 39 | + triggerCharacters: ['"', "'", '`', ' ', '.', '(', '[', ']', '!', '/', '-', ':'], |
| 40 | + }, |
| 41 | + }), |
| 42 | + ]), |
| 43 | + ) |
| 44 | + |
| 45 | + let countBeforeChange = client.serverCapabilities.length |
| 46 | + let capabilitiesDidChange = Promise.race([ |
| 47 | + new Promise<void>((_, reject) => { |
| 48 | + setTimeout(() => reject('capabilities did not change within 5s'), 5_000) |
| 49 | + }), |
| 50 | + |
| 51 | + new Promise<void>((resolve) => { |
| 52 | + client.onServerCapabilitiesChanged(() => { |
| 53 | + if (client.serverCapabilities.length !== countBeforeChange) return |
| 54 | + resolve() |
| 55 | + }) |
| 56 | + }), |
| 57 | + ]) |
| 58 | + |
| 59 | + await fs.writeFile( |
| 60 | + `${root}/tailwind.config.js`, |
| 61 | + js` |
| 62 | + module.exports = { |
| 63 | + separator: '_', |
| 64 | + } |
| 65 | + `, |
| 66 | + ) |
| 67 | + |
| 68 | + // After changing the config |
| 69 | + client.notifyChangedFiles({ |
| 70 | + changed: [`${root}/tailwind.config.js`], |
| 71 | + }) |
| 72 | + |
| 73 | + // We should see that the capabilities have changed |
| 74 | + await capabilitiesDidChange |
| 75 | + |
| 76 | + // Capabilities are now registered |
| 77 | + expect(client.serverCapabilities).toContainEqual( |
| 78 | + expect.objectContaining({ |
| 79 | + method: 'textDocument/hover', |
| 80 | + }), |
| 81 | + ) |
| 82 | + |
| 83 | + expect(client.serverCapabilities).toContainEqual( |
| 84 | + expect.objectContaining({ |
| 85 | + method: 'textDocument/completion', |
| 86 | + registerOptions: { |
| 87 | + documentSelector: null, |
| 88 | + resolveProvider: true, |
| 89 | + triggerCharacters: ['"', "'", '`', ' ', '.', '(', '[', ']', '!', '/', '-', '_'], |
| 90 | + }, |
| 91 | + }), |
| 92 | + ) |
| 93 | + |
| 94 | + expect(client.serverCapabilities).not.toContainEqual( |
| 95 | + expect.objectContaining({ |
| 96 | + method: 'textDocument/completion', |
| 97 | + registerOptions: { |
| 98 | + documentSelector: null, |
| 99 | + resolveProvider: true, |
| 100 | + triggerCharacters: ['"', "'", '`', ' ', '.', '(', '[', ']', '!', '/', '-', ':'], |
| 101 | + }, |
| 102 | + }), |
| 103 | + ) |
| 104 | + }, |
| 105 | +}) |
0 commit comments