DiffViewProvider.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { DiffViewProvider } from "../DiffViewProvider"
  2. import * as vscode from "vscode"
  3. // Mock vscode
  4. jest.mock("vscode", () => ({
  5. workspace: {
  6. applyEdit: jest.fn(),
  7. },
  8. window: {
  9. createTextEditorDecorationType: jest.fn(),
  10. },
  11. WorkspaceEdit: jest.fn().mockImplementation(() => ({
  12. replace: jest.fn(),
  13. delete: jest.fn(),
  14. })),
  15. Range: jest.fn(),
  16. Position: jest.fn(),
  17. Selection: jest.fn(),
  18. TextEditorRevealType: {
  19. InCenter: 2,
  20. },
  21. }))
  22. // Mock DecorationController
  23. jest.mock("../DecorationController", () => ({
  24. DecorationController: jest.fn().mockImplementation(() => ({
  25. setActiveLine: jest.fn(),
  26. updateOverlayAfterLine: jest.fn(),
  27. clear: jest.fn(),
  28. })),
  29. }))
  30. describe("DiffViewProvider", () => {
  31. let diffViewProvider: DiffViewProvider
  32. const mockCwd = "/mock/cwd"
  33. let mockWorkspaceEdit: { replace: jest.Mock; delete: jest.Mock }
  34. beforeEach(() => {
  35. jest.clearAllMocks()
  36. mockWorkspaceEdit = {
  37. replace: jest.fn(),
  38. delete: jest.fn(),
  39. }
  40. ;(vscode.WorkspaceEdit as jest.Mock).mockImplementation(() => mockWorkspaceEdit)
  41. diffViewProvider = new DiffViewProvider(mockCwd)
  42. // Mock the necessary properties and methods
  43. ;(diffViewProvider as any).relPath = "test.txt"
  44. ;(diffViewProvider as any).activeDiffEditor = {
  45. document: {
  46. uri: { fsPath: `${mockCwd}/test.txt` },
  47. getText: jest.fn(),
  48. lineCount: 10,
  49. },
  50. selection: {
  51. active: { line: 0, character: 0 },
  52. anchor: { line: 0, character: 0 },
  53. },
  54. edit: jest.fn().mockResolvedValue(true),
  55. revealRange: jest.fn(),
  56. }
  57. ;(diffViewProvider as any).activeLineController = { setActiveLine: jest.fn(), clear: jest.fn() }
  58. ;(diffViewProvider as any).fadedOverlayController = { updateOverlayAfterLine: jest.fn(), clear: jest.fn() }
  59. })
  60. describe("update method", () => {
  61. it("should preserve empty last line when original content has one", async () => {
  62. ;(diffViewProvider as any).originalContent = "Original content\n"
  63. await diffViewProvider.update("New content", true)
  64. expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
  65. expect.anything(),
  66. expect.anything(),
  67. "New content\n",
  68. )
  69. })
  70. it("should not add extra newline when accumulated content already ends with one", async () => {
  71. ;(diffViewProvider as any).originalContent = "Original content\n"
  72. await diffViewProvider.update("New content\n", true)
  73. expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
  74. expect.anything(),
  75. expect.anything(),
  76. "New content\n",
  77. )
  78. })
  79. it("should not add newline when original content does not end with one", async () => {
  80. ;(diffViewProvider as any).originalContent = "Original content"
  81. await diffViewProvider.update("New content", true)
  82. expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(expect.anything(), expect.anything(), "New content")
  83. })
  84. })
  85. })