f3b8446cb22e57813eaa367139251aa6d36b9f85df9491309934f45f25325285cf5bdaf52eec34dd8cb70d115b85bff9b68b00fb93b0bee6209983156c9230 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import staticRegister, {collection} from 'handsontable/utils/staticRegister';
  2. describe('staticRegister', () => {
  3. describe('.register/.getItem', () => {
  4. afterEach(() => {
  5. collection.clear();
  6. });
  7. it('should register item to the collection and make it overwritable', () => {
  8. const sr = staticRegister();
  9. const testObject = {foo: 'bar'};
  10. sr.register('name', 'baz');
  11. sr.register('name', testObject);
  12. expect(sr.getItem('name')).toBe(testObject);
  13. });
  14. });
  15. describe('.hasItem', () => {
  16. afterEach(() => {
  17. collection.clear();
  18. });
  19. it('should return `true` when item exists in the collection', () => {
  20. const sr = staticRegister();
  21. const testObject = {foo: 'bar'};
  22. sr.register('name', testObject);
  23. expect(sr.hasItem('name')).toBe(true);
  24. });
  25. it('should return `false` when item not exist in the collection', () => {
  26. const sr = staticRegister();
  27. const testObject = {foo: 'bar'};
  28. sr.register('name', testObject);
  29. expect(sr.hasItem('wrong_name')).toBe(false);
  30. });
  31. });
  32. describe('.getNames', () => {
  33. afterEach(() => {
  34. collection.clear();
  35. });
  36. it('should return an empty array when no items was registered', () => {
  37. const sr = staticRegister();
  38. expect(sr.getNames()).toEqual([]);
  39. });
  40. it('should return an array of string as a list of registered names', () => {
  41. const sr = staticRegister();
  42. sr.register('name', {foo: 'bar'});
  43. sr.register('number', 1);
  44. sr.register('string', 'baz');
  45. expect(sr.getNames()).toEqual(['name', 'number', 'string']);
  46. });
  47. });
  48. describe('.getValues', () => {
  49. afterEach(() => {
  50. collection.clear();
  51. });
  52. it('should return an empty array when no items was registered', () => {
  53. const sr = staticRegister();
  54. expect(sr.getValues()).toEqual([]);
  55. });
  56. it('should return an array of string as a list of registered items', () => {
  57. const sr = staticRegister();
  58. sr.register('name', {foo: 'bar'});
  59. sr.register('number', 1);
  60. sr.register('string', 'baz');
  61. expect(sr.getValues()).toEqual([{foo: 'bar'}, 1, 'baz']);
  62. });
  63. });
  64. it('should share items between the same namespaces and make them overwritable', () => {
  65. const sr1 = staticRegister('my-space');
  66. const sr2 = staticRegister('my-space');
  67. const sr3 = staticRegister();
  68. sr1.register('foo', 'bar');
  69. sr2.register('foo', 'baz');
  70. sr3.register('foo', 'test');
  71. expect(sr1.getValues()).toEqual(['baz']);
  72. expect(sr2.getValues()).toEqual(['baz']);
  73. expect(sr3.getValues()).toEqual(['test']);
  74. });
  75. });