Collection.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Entry from '../../../src/lib/Entry';
  2. import Collection from '../../../src/lib/Collection';
  3. describe('Collection', () => {
  4. const directory = {
  5. directory: true,
  6. fullPath: '/path/to/',
  7. modified: Date.now(),
  8. },
  9. file1 = {
  10. directory: false,
  11. fullPath: '/path/to/file2.txt',
  12. modified: Date.now(),
  13. size: 54321,
  14. mimeType: 'text/plain',
  15. },
  16. entries = [
  17. directory,
  18. file1,
  19. {
  20. directory: false,
  21. fullPath: '/path/to/file1.txt',
  22. modified: Date.now(),
  23. size: 12345,
  24. mimeType: 'text/plain',
  25. },
  26. ],
  27. collection = new Collection(entries),
  28. collectionEntries = collection.map((entry) => entry);
  29. it('should the expected path from the first entry', () => {
  30. expect(collection.path()).toBe('/path/to');
  31. });
  32. it('should create a new parent entry from the original first item', () => {
  33. expect(collectionEntries[0].fullPath).not.toBe(entries[0].fullPath);
  34. });
  35. it('should sort the entries by name', () => {
  36. expect(collectionEntries[1].fullPath).toBe(entries[2].fullPath);
  37. });
  38. it('should be possible to filter the entries', () => {
  39. expect(
  40. collection.filter((entry) => entry.name === 'file1.txt').length
  41. ).toBe(1);
  42. });
  43. it('should be possible to add a new Entry', () => {
  44. collection.add(
  45. new Entry({
  46. ...file1,
  47. fullPath: '/path/to/file3.txt',
  48. })
  49. );
  50. expect(
  51. collection.filter((entry) => entry.name === 'file3.txt').length
  52. ).toBe(1);
  53. });
  54. it('should be sort the entries alphabetically after adding a new Entry', () => {
  55. collection.add(
  56. new Entry({
  57. ...file1,
  58. fullPath: '/path/to/file0.txt',
  59. })
  60. );
  61. collection.forEach((entry, i) => {
  62. if (entry.name === 'file0.txt') {
  63. expect(i).toBe(1);
  64. }
  65. });
  66. });
  67. it('should be possible to remove an Entry', () => {
  68. const [file0] = collection.filter((entry) => entry.name === 'file0.txt');
  69. collection.remove(file0);
  70. expect(
  71. collection.filter((entry) => entry.name === 'file0.txt').length
  72. ).toBe(0);
  73. });
  74. });