index.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* eslint consistent-return: 0 */
  2. const chai = require('chai');
  3. const fs = require('fs');
  4. const rimraf = require('rimraf');
  5. const path = require('path');
  6. const proxyquire = require('proxyquire');
  7. const fakeAppDataDir = path.join(process.cwd(), 'fake-appData');
  8. const mockGetElectronFullPath = (filePath) => path.join('fake-appData', filePath);
  9. const storage = proxyquire('../../dist/index', {
  10. './utils': {
  11. processPath: mockGetElectronFullPath,
  12. processPathNoJson: mockGetElectronFullPath,
  13. },
  14. });
  15. describe('Electron Storage', () => {
  16. beforeEach('create the fake-appData folder', () => {
  17. fs.mkdirSync(fakeAppDataDir);
  18. });
  19. afterEach('delete the fake-appData folder', () => {
  20. rimraf.sync(fakeAppDataDir);
  21. });
  22. describe('storage.get()', () => {
  23. it('receives the data from a json file', (done) => {
  24. const json = JSON.stringify({ awesome: 'data' });
  25. fs.writeFile(path.join(fakeAppDataDir, 'my-awesome-data.json'), json, (err) => {
  26. if (err) {
  27. throw err;
  28. }
  29. storage.get('my-awesome-data.json', (error, data) => {
  30. chai.expect(error).to.equal(null);
  31. chai.expect(data).to.deep.equal({ awesome: 'data' });
  32. done();
  33. });
  34. });
  35. });
  36. });
  37. describe('storage.get()', () => {
  38. it('receives the data from a json file', (done) => {
  39. const json = JSON.stringify({ awesome: 'data' });
  40. fs.writeFile(path.join(fakeAppDataDir, 'my-awesome-data.json'), json, (err) => {
  41. if (err) {
  42. throw err;
  43. }
  44. storage.get('my-awesome-data.json').then(data => {
  45. chai.expect(data).to.deep.equal({ awesome: 'data' });
  46. done();
  47. });
  48. });
  49. });
  50. });
  51. describe('storage.set()', () => {
  52. it('sets a json file that you can later get', (done) => {
  53. storage.set('data.json', { js: 'on' }, (err) => {
  54. chai.expect(err).to.equal(null);
  55. storage.get('data.json', (error, data) => {
  56. chai.expect(data).to.deep.equal({ js: 'on' });
  57. done();
  58. });
  59. });
  60. });
  61. it('creates folders if needed', (done) => {
  62. storage.set(path.join('in', 'some', 'folders', 'data.json'), { js: 'on' }, (err) => {
  63. chai.expect(err).to.equal(null);
  64. storage.get(path.join('in', 'some', 'folders', 'data.json'), (error, data) => {
  65. chai.expect(data).to.deep.equal({ js: 'on' });
  66. done();
  67. });
  68. });
  69. });
  70. });
  71. describe('storage.isPathExists()', () => {
  72. it('return true if the path exists', (done) => {
  73. storage.set(path.join('in', 'some', 'folders', 'data.json'), { js: 'on' }, (err) => {
  74. chai.expect(err).to.equal(null);
  75. storage.isPathExists(path.join('in', 'some', 'folders', 'data.json'), (data) => {
  76. chai.expect(data).to.equal(true);
  77. done();
  78. });
  79. });
  80. });
  81. it('return false if the path doesn\'t exists', (done) => {
  82. storage.set(path.join('in', 'some', 'folders', 'data.json'), { js: 'on' }, (err) => {
  83. chai.expect(err).to.equal(null);
  84. storage.isPathExists(path.join('in', 'some', 'other', 'folders', 'data.json'), (data) => {
  85. chai.expect(data).to.equal(false);
  86. done();
  87. });
  88. });
  89. });
  90. });
  91. describe('storage.remove() - file', () => {
  92. it('removes the file in path', (done) => {
  93. storage.set(path.join('in', 'some', 'folders', 'data.json'), { js: 'on' }, (err) => {
  94. chai.expect(err).to.equal(null);
  95. storage.remove(path.join('in', 'some', 'folders', 'data.json'), error => {
  96. storage.isPathExists(path.join('in', 'some', 'other', 'folders', 'data.json'), (data) => {
  97. chai.expect(data).to.equal(false);
  98. chai.expect(error).to.equal(null);
  99. done();
  100. });
  101. });
  102. });
  103. });
  104. });
  105. describe('storage.remove() - folder', () => {
  106. it('removes the folder in path', (done) => {
  107. storage.set(path.join('in', 'some', 'folders', 'data.json'), { js: 'on' }, (err) => {
  108. chai.expect(err).to.equal(null);
  109. storage.remove(path.join('in', 'some', 'folders'), error => {
  110. storage.isPathExists(path.join('in', 'some', 'folders'), (data) => {
  111. chai.expect(data).to.equal(false);
  112. chai.expect(error).to.equal(null);
  113. done();
  114. });
  115. });
  116. });
  117. });
  118. });
  119. });