get-active-path-spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const path = require('path')
  2. const getActivePath = require('../lib/get-active-path')
  3. const { it, fit, beforeEach, afterEach } = require('./async-spec-helpers') // eslint-disable-line no-unused-vars
  4. const projectPath = path.resolve(__dirname, './fixtures/project/')
  5. const file1 = path.resolve(__dirname, './fixtures/project/file1.txt')
  6. const file2 = path.resolve(__dirname, './fixtures/project/file2.txt')
  7. const img1 = path.resolve(__dirname, './fixtures/project/img1.png')
  8. describe('getActivePath', function () {
  9. let workspaceElement
  10. beforeEach(async function () {
  11. workspaceElement = atom.views.getView(atom.workspace)
  12. await atom.packages.activatePackage('tabs')
  13. await atom.packages.activatePackage('tree-view')
  14. atom.project.setPaths([projectPath])
  15. })
  16. it('returns project path when no target', function () {
  17. const itemPath = getActivePath()
  18. expect(itemPath).toBe(projectPath)
  19. })
  20. it('returns project path when nothing open', function () {
  21. const itemPath = getActivePath(workspaceElement)
  22. expect(itemPath).toBe(projectPath)
  23. })
  24. it('returns active file path when workspace is selected', async function () {
  25. await atom.workspace.open(file1)
  26. await atom.workspace.open(file2)
  27. const itemPath = getActivePath(workspaceElement)
  28. expect(itemPath).toBe(file2)
  29. })
  30. it('returns file path when tree view is selected', async function () {
  31. await atom.workspace.open(file1)
  32. await atom.workspace.open(file2)
  33. const { treeView } = atom.packages.getLoadedPackage('tree-view').mainModule
  34. const file1Target = treeView.selectEntryForPath(file1)
  35. const itemPath = getActivePath(file1Target)
  36. expect(itemPath).toBe(file1)
  37. })
  38. it('returns file path when tab is selected', async function () {
  39. await atom.workspace.open(file1)
  40. await atom.workspace.open(file2)
  41. const file1Target = workspaceElement.querySelector(".tab-bar [data-name='file1.txt']")
  42. const itemPath = getActivePath(file1Target)
  43. expect(itemPath).toBe(file1)
  44. })
  45. it('returns project when active pane is not a file', async function () {
  46. await atom.packages.activatePackage('settings-view')
  47. await atom.workspace.open(file1)
  48. await atom.workspace.open('atom://config')
  49. const itemPath = getActivePath(workspaceElement)
  50. expect(itemPath).toBe(projectPath)
  51. })
  52. it('returns active pane path when it is not a text file', async function () {
  53. await atom.packages.activatePackage('image-view')
  54. await atom.workspace.open(file1)
  55. await atom.workspace.open(img1)
  56. const itemPath = getActivePath(workspaceElement)
  57. expect(itemPath).toBe(img1)
  58. })
  59. })