|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import type * as vscode from 'vscode'; |
| 3 | +import * as prettier from 'prettier'; |
| 4 | +import { vscodePrettierFormat, nodePrettierFormat } from './formatter'; |
| 5 | + |
| 6 | +vi.mock('prettier', () => ({ |
| 7 | + format: vi.fn(), |
| 8 | + getFileInfo: vi.fn(), |
| 9 | + resolveConfig: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('vscode', () => ({ |
| 13 | + workspace: { |
| 14 | + getWorkspaceFolder: vi.fn(() => ({ |
| 15 | + uri: { fsPath: '/workspace' }, |
| 16 | + })), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +describe('formatter', () => { |
| 21 | + const localFile = { |
| 22 | + uri: { |
| 23 | + scheme: 'file', |
| 24 | + fsPath: '/snippets/button.liquid', |
| 25 | + }, |
| 26 | + getText: vi.fn(() => '<div>{{ product.title }}</div>'), |
| 27 | + } as unknown as vscode.TextDocument; |
| 28 | + |
| 29 | + const remoteFile = { |
| 30 | + ...localFile, |
| 31 | + uri: { ...localFile.uri, scheme: 'vscode-remote' }, |
| 32 | + } as vscode.TextDocument; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + vi.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('vscodePrettierFormat', () => { |
| 39 | + it('should format remote files without config options', async () => { |
| 40 | + await vscodePrettierFormat(remoteFile); |
| 41 | + |
| 42 | + expect(prettier.getFileInfo).not.toHaveBeenCalled(); |
| 43 | + expect(prettier.resolveConfig).not.toHaveBeenCalled(); |
| 44 | + expect(prettier.format).toHaveBeenCalledWith('<div>{{ product.title }}</div>', { |
| 45 | + parser: 'liquid-html', |
| 46 | + plugins: [expect.anything()], |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should format local files with config options', async () => { |
| 51 | + await vscodePrettierFormat(localFile); |
| 52 | + |
| 53 | + expect(prettier.getFileInfo).toHaveBeenCalled(); |
| 54 | + expect(prettier.resolveConfig).toHaveBeenCalled(); |
| 55 | + expect(prettier.format).toHaveBeenCalledWith('<div>{{ product.title }}</div>', { |
| 56 | + parser: 'liquid-html', |
| 57 | + plugins: [expect.anything()], |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should not format ignored local files', async () => { |
| 62 | + vi.mocked(prettier.getFileInfo).mockResolvedValue({ |
| 63 | + ignored: true, |
| 64 | + } as prettier.FileInfoResult); |
| 65 | + |
| 66 | + const result = await nodePrettierFormat(localFile); |
| 67 | + |
| 68 | + expect(prettier.getFileInfo).toHaveBeenCalled(); |
| 69 | + expect(prettier.resolveConfig).not.toHaveBeenCalled(); |
| 70 | + expect(prettier.format).not.toHaveBeenCalled(); |
| 71 | + expect(result).toBe('<div>{{ product.title }}</div>'); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments