Collection.test.js 2.1 KB

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