update-package-dependencies-spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const os = require('os');
  2. const path = require('path');
  3. const updatePackageDependencies = require('../lib/update-package-dependencies');
  4. describe('Update Package Dependencies', () => {
  5. let projectPath = null;
  6. beforeEach(() => {
  7. projectPath = __dirname;
  8. atom.project.setPaths([projectPath]);
  9. });
  10. describe('updating package dependencies', () => {
  11. let { command, args, stderr, exit, options } = {};
  12. beforeEach(() => {
  13. spyOn(updatePackageDependencies, 'runBufferedProcess').andCallFake(
  14. params => {
  15. ({ command, args, stderr, exit, options } = params);
  16. return true; // so that this.process isn't null
  17. }
  18. );
  19. });
  20. afterEach(() => {
  21. if (updatePackageDependencies.process) exit(0);
  22. });
  23. it('runs the `apm install` command', () => {
  24. updatePackageDependencies.update();
  25. expect(updatePackageDependencies.runBufferedProcess).toHaveBeenCalled();
  26. if (process.platform !== 'win32') {
  27. expect(command.endsWith('/apm')).toBe(true);
  28. } else {
  29. expect(command.endsWith('\\apm.cmd')).toBe(true);
  30. }
  31. expect(args).toEqual(['install', '--no-color']);
  32. expect(options.cwd).toEqual(projectPath);
  33. });
  34. it('only allows one apm process to be spawned at a time', () => {
  35. updatePackageDependencies.update();
  36. expect(updatePackageDependencies.runBufferedProcess.callCount).toBe(1);
  37. updatePackageDependencies.update();
  38. updatePackageDependencies.update();
  39. expect(updatePackageDependencies.runBufferedProcess.callCount).toBe(1);
  40. exit(0);
  41. updatePackageDependencies.update();
  42. expect(updatePackageDependencies.runBufferedProcess.callCount).toBe(2);
  43. });
  44. it('sets NODE_ENV to development in order to install devDependencies', () => {
  45. updatePackageDependencies.update();
  46. expect(options.env.NODE_ENV).toEqual('development');
  47. });
  48. it('adds a status bar tile', async () => {
  49. const statusBar = await atom.packages.activatePackage('status-bar');
  50. const activationPromise = atom.packages.activatePackage(
  51. 'update-package-dependencies'
  52. );
  53. atom.commands.dispatch(
  54. atom.views.getView(atom.workspace),
  55. 'update-package-dependencies:update'
  56. );
  57. const { mainModule } = await activationPromise;
  58. mainModule.update();
  59. let tile = statusBar.mainModule.statusBar
  60. .getRightTiles()
  61. .find(tile => tile.item.matches('update-package-dependencies-status'));
  62. expect(
  63. tile.item.classList.contains('update-package-dependencies-status')
  64. ).toBe(true);
  65. expect(tile.item.firstChild.classList.contains('loading')).toBe(true);
  66. exit(0);
  67. tile = statusBar.mainModule.statusBar
  68. .getRightTiles()
  69. .find(tile => tile.item.matches('update-package-dependencies-status'));
  70. expect(tile).toBeUndefined();
  71. });
  72. describe('when there are multiple project paths', () => {
  73. beforeEach(() => atom.project.setPaths([os.tmpdir(), projectPath]));
  74. it('uses the currently active one', async () => {
  75. await atom.workspace.open(path.join(projectPath, 'package.json'));
  76. updatePackageDependencies.update();
  77. expect(options.cwd).toEqual(projectPath);
  78. });
  79. });
  80. describe('when the update succeeds', () => {
  81. beforeEach(() => {
  82. updatePackageDependencies.update();
  83. exit(0);
  84. });
  85. it('shows a success notification message', () => {
  86. const notification = atom.notifications.getNotifications()[0];
  87. expect(notification.getType()).toEqual('success');
  88. expect(notification.getMessage()).toEqual(
  89. 'Package dependencies updated'
  90. );
  91. });
  92. });
  93. describe('when the update fails', () => {
  94. beforeEach(() => {
  95. updatePackageDependencies.update();
  96. stderr('oh bother');
  97. exit(127);
  98. });
  99. it('shows a failure notification', () => {
  100. const notification = atom.notifications.getNotifications()[0];
  101. expect(notification.getType()).toEqual('error');
  102. expect(notification.getMessage()).toEqual(
  103. 'Failed to update package dependencies'
  104. );
  105. expect(notification.getDetail()).toEqual('oh bother');
  106. expect(notification.isDismissable()).toBe(true);
  107. });
  108. });
  109. });
  110. describe('the `update-package-dependencies:update` command', () => {
  111. beforeEach(() => spyOn(updatePackageDependencies, 'update'));
  112. it('activates the package and updates package dependencies', async () => {
  113. const activationPromise = atom.packages.activatePackage(
  114. 'update-package-dependencies'
  115. );
  116. atom.commands.dispatch(
  117. atom.views.getView(atom.workspace),
  118. 'update-package-dependencies:update'
  119. );
  120. const { mainModule } = await activationPromise;
  121. expect(mainModule.update).toHaveBeenCalled();
  122. });
  123. });
  124. });