fs.helpers.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import fs from 'fs-extra';
  2. import { readJsonFile, readFile, readdirSync, fileExists, writeFile, createFolder, deleteFolder, getSeed } from '../fs.helpers';
  3. jest.mock('fs-extra');
  4. beforeEach(() => {
  5. // @ts-ignore
  6. fs.__resetAllMocks();
  7. });
  8. describe('Test: readJsonFile', () => {
  9. it('should return the json file', () => {
  10. // Arrange
  11. const rawFile = '{"test": "test"}';
  12. const mockFiles = {
  13. '/runtipi/test-file.json': rawFile,
  14. };
  15. // @ts-ignore
  16. fs.__createMockFiles(mockFiles);
  17. // Act
  18. const file = readJsonFile('/runtipi/test-file.json');
  19. // Assert
  20. expect(file).toEqual({ test: 'test' });
  21. });
  22. it('should return null if the file does not exist', () => {
  23. expect(readJsonFile('/test')).toBeNull();
  24. });
  25. it('Should return null if fs.readFile throws an error', () => {
  26. // Arrange
  27. // @ts-ignore
  28. const spy = jest.spyOn(fs, 'readFileSync');
  29. spy.mockImplementation(() => {
  30. throw new Error('Error');
  31. });
  32. // Act
  33. const file = readJsonFile('/test');
  34. // Assert
  35. expect(file).toBeNull();
  36. spy.mockRestore();
  37. });
  38. });
  39. describe('Test: readFile', () => {
  40. it('should return the file', () => {
  41. const rawFile = 'test';
  42. const mockFiles = {
  43. '/runtipi/test-file.txt': rawFile,
  44. };
  45. // @ts-ignore
  46. fs.__createMockFiles(mockFiles);
  47. expect(readFile('/runtipi/test-file.txt')).toEqual('test');
  48. });
  49. it('should return empty string if the file does not exist', () => {
  50. expect(readFile('/test')).toEqual('');
  51. });
  52. });
  53. describe('Test: readdirSync', () => {
  54. it('should return the files', () => {
  55. const mockFiles = {
  56. '/runtipi/test/test-file.txt': 'test',
  57. };
  58. // @ts-ignore
  59. fs.__createMockFiles(mockFiles);
  60. expect(readdirSync('/runtipi/test')).toEqual(['test-file.txt']);
  61. });
  62. it('should return empty array if the directory does not exist', () => {
  63. expect(readdirSync('/test')).toEqual([]);
  64. });
  65. });
  66. describe('Test: fileExists', () => {
  67. it('should return true if the file exists', () => {
  68. const mockFiles = {
  69. '/runtipi/test-file.txt': 'test',
  70. };
  71. // @ts-ignore
  72. fs.__createMockFiles(mockFiles);
  73. expect(fileExists('/runtipi/test-file.txt')).toBeTruthy();
  74. });
  75. it('should return false if the file does not exist', () => {
  76. expect(fileExists('/test-file.txt')).toBeFalsy();
  77. });
  78. });
  79. describe('Test: writeFile', () => {
  80. it('should write the file', () => {
  81. const spy = jest.spyOn(fs, 'writeFileSync');
  82. writeFile('/runtipi/test-file.txt', 'test');
  83. expect(spy).toHaveBeenCalledWith('/runtipi/test-file.txt', 'test');
  84. });
  85. });
  86. describe('Test: createFolder', () => {
  87. it('should create the folder', () => {
  88. const spy = jest.spyOn(fs, 'mkdirSync');
  89. createFolder('/test');
  90. expect(spy).toHaveBeenCalledWith('/test', { recursive: true });
  91. });
  92. });
  93. describe('Test: deleteFolder', () => {
  94. it('should delete the folder', () => {
  95. const spy = jest.spyOn(fs, 'rmSync');
  96. deleteFolder('/test');
  97. expect(spy).toHaveBeenCalledWith('/test', { recursive: true });
  98. });
  99. });
  100. describe('Test: getSeed', () => {
  101. it('should return the seed', () => {
  102. const mockFiles = {
  103. '/runtipi/state/seed': 'test',
  104. };
  105. // @ts-ignore
  106. fs.__createMockFiles(mockFiles);
  107. expect(getSeed()).toEqual('test');
  108. });
  109. });