resourcerer 1.2.3 → 2.0.0-beta-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +133 -164
  2. package/build/index.d.ts +9 -0
  3. package/build/index.js +9 -0
  4. package/build/lib/collection.d.ts +107 -0
  5. package/build/lib/collection.js +251 -0
  6. package/build/lib/config.d.ts +20 -0
  7. package/build/lib/config.js +23 -0
  8. package/build/lib/error-boundary.d.ts +12 -0
  9. package/build/lib/error-boundary.js +18 -0
  10. package/build/lib/events.d.ts +11 -0
  11. package/build/lib/events.js +12 -0
  12. package/build/lib/model-cache.d.ts +13 -0
  13. package/build/lib/model-cache.js +62 -0
  14. package/build/lib/model.d.ts +48 -0
  15. package/build/lib/model.js +170 -0
  16. package/build/lib/prefetch.d.ts +3 -0
  17. package/build/lib/prefetch.js +27 -0
  18. package/build/lib/request.d.ts +20 -0
  19. package/build/lib/request.js +53 -0
  20. package/build/lib/resourcerer.d.ts +13 -0
  21. package/build/lib/resourcerer.js +406 -0
  22. package/build/lib/sync.d.ts +18 -0
  23. package/build/lib/sync.js +58 -0
  24. package/build/lib/types.d.ts +66 -0
  25. package/build/lib/types.js +1 -0
  26. package/build/lib/utils.d.ts +14 -0
  27. package/build/lib/utils.js +87 -0
  28. package/docs/advanced_topics.md +9 -77
  29. package/docs/collection.md +28 -28
  30. package/docs/migrating_to_2.0.md +72 -0
  31. package/docs/model.md +21 -21
  32. package/docs/typescript.md +72 -22
  33. package/package.json +27 -30
  34. package/resourcerer.d.ts +58 -0
  35. package/test/collection.test.js +305 -316
  36. package/test/config.test.js +21 -75
  37. package/test/events.test.js +42 -41
  38. package/test/model-cache.test.js +99 -77
  39. package/test/model.test.js +204 -209
  40. package/test/prefetch.test.js +35 -61
  41. package/test/request.test.js +104 -109
  42. package/test/sync.test.js +122 -109
  43. package/test/utils.test.js +173 -159
  44. package/index.d.ts +0 -270
  45. package/index.js +0 -14
  46. package/lib/collection.js +0 -584
  47. package/lib/config.js +0 -131
  48. package/lib/constants.js +0 -6
  49. package/lib/error-boundary.js +0 -47
  50. package/lib/events.js +0 -19
  51. package/lib/model-cache.js +0 -131
  52. package/lib/model.js +0 -372
  53. package/lib/prefetch.js +0 -59
  54. package/lib/request.js +0 -126
  55. package/lib/resourcerer.js +0 -985
  56. package/lib/sync.js +0 -111
  57. package/lib/utils.js +0 -220
  58. package/test/model-mocks.js +0 -110
  59. package/test/setup.js +0 -63
@@ -1,115 +1,61 @@
1
- import * as Config from '../lib/config';
2
- import Model from '../lib/model';
3
- import {noOp} from '../lib/utils';
1
+ import * as Config from "../lib/config";
2
+ import Model from "../lib/model";
3
+ import { noOp } from "../lib/utils";
4
+ import { vi } from "vitest";
4
5
 
5
6
  class TestModel extends Model {}
6
7
  class TestModel2 extends Model {}
7
8
 
9
+ const { register } = Config;
10
+
8
11
  /* eslint-disable max-nested-callbacks */
9
- describe('Config', () => {
12
+ describe("Config", () => {
10
13
  afterEach(() => {
11
- delete Config.ResourceKeys.TEST_MODEL;
12
- delete Config.ResourceKeys.TEST_MODEL2;
13
- });
14
-
15
- describe('#addResourceKeys', () => {
16
- it('adds keys to the ResourceKeys object', () => {
17
- expect(Config.ResourceKeys.TEST_MODEL).not.toBeDefined();
18
- expect(Config.ResourceKeys.TEST_MODEL2).not.toBeDefined();
19
-
20
- Config.ResourceKeys.add({TEST_MODEL: 'testModel', TEST_MODEL2: 'testModel2'});
21
-
22
- expect(Config.ResourceKeys.TEST_MODEL).toEqual('testModel');
23
- expect(Config.ResourceKeys.TEST_MODEL2).toEqual('testModel2');
24
- });
25
- });
26
-
27
- describe('#addModels', () => {
28
- beforeEach(() => {
29
- expect(Config.ModelMap.testModel).not.toBeDefined();
30
- expect(Config.ModelMap.testModel2).not.toBeDefined();
31
- });
32
-
33
- afterEach(() => {
34
- delete Config.ModelMap.testModel;
35
- delete Config.ModelMap.testModel2;
36
- });
37
-
38
- it('adds models passed as an object to the model map', () => {
39
- Config.ModelMap.add({testModel: TestModel, testModel2: TestModel2});
40
-
41
- expect(Config.ModelMap.testModel).toEqual(TestModel);
42
- expect(Config.ModelMap.testModel2).toEqual(TestModel2);
43
- });
44
-
45
- it('adds camelized resource keys if they do not already exist', () => {
46
- Config.ModelMap.add({TEST_MODEL: TestModel, TEST_MODEL2: TestModel2});
47
-
48
- expect(Config.ModelMap.testModel).toEqual(TestModel);
49
- expect(Config.ModelMap.testModel2).toEqual(TestModel2);
50
- expect(Config.ResourceKeys.TEST_MODEL).toEqual('testModel');
51
- expect(Config.ResourceKeys.TEST_MODEL2).toEqual('testModel2');
52
- });
53
-
54
- it('does not overwrite any manually-added resource keys', () => {
55
- Config.ResourceKeys.add({TEST_MODEL2: 'customName', TEST_MODEL3: 'testModel3'});
56
- Config.ModelMap.add({TEST_MODEL: TestModel, TEST_MODEL2: TestModel2});
57
-
58
- expect(Config.ResourceKeys.TEST_MODEL).toEqual('testModel');
59
- expect(Config.ResourceKeys.TEST_MODEL2).toEqual('customName');
60
- expect(Config.ResourceKeys.TEST_MODEL3).toEqual('testModel3');
14
+ Object.keys(Config.ModelMap).forEach((resourceKey) => {
15
+ delete Config.ModelMap[resourceKey];
61
16
  });
62
17
  });
63
18
 
64
- describe('adding UnfetchedResources', () => {
65
- it('adds a resource to the unfetched list', () => {
66
- Config.ResourceKeys.add({TEST_MODEL: 'testModel', TEST_MODEL2: 'testModel2'});
67
- expect(Config.UnfetchedResources.has(Config.ResourceKeys.TEST_MODEL)).toBe(false);
68
- expect(Config.UnfetchedResources.has(Config.ResourceKeys.TEST_MODEL2)).toBe(false);
69
-
70
- Config.UnfetchedResources.add(Config.ResourceKeys.TEST_MODEL);
71
- Config.UnfetchedResources.add(Config.ResourceKeys.TEST_MODEL2);
19
+ describe("#register", () => {
20
+ it("adds keys to the ModelMap object", () => {
21
+ expect(Config.ModelMap.TEST_MODEL).not.toBeDefined();
22
+ expect(Config.ModelMap.TEST_MODEL2).not.toBeDefined();
72
23
 
73
- expect(Config.UnfetchedResources.has(Config.ResourceKeys.TEST_MODEL)).toBe(true);
74
- expect(Config.UnfetchedResources.has(Config.ResourceKeys.TEST_MODEL2)).toBe(true);
24
+ register({ TEST_MODEL: "testModel", TEST_MODEL2: "testModel2" });
75
25
 
76
- Config.UnfetchedResources.delete(Config.ResourceKeys.TEST_MODEL);
77
- Config.UnfetchedResources.delete(Config.ResourceKeys.TEST_MODEL2);
26
+ expect(Config.ModelMap.TEST_MODEL).toEqual("testModel");
27
+ expect(Config.ModelMap.TEST_MODEL2).toEqual("testModel2");
78
28
  });
79
29
  });
80
30
 
81
- describe('#setConfig', () => {
82
- it('adds a setting to the configs, overriding defaults', () => {
83
- var logSpy = jest.fn(),
84
- trackSpy = jest.fn(),
85
- prefilterSpy = jest.fn();
31
+ describe("#setConfig", () => {
32
+ it("adds a setting to the configs, overriding defaults", () => {
33
+ var logSpy = vi.fn(),
34
+ trackSpy = vi.fn(),
35
+ prefilterSpy = vi.fn();
86
36
 
87
37
  expect(Config.ResourcesConfig.cacheGracePeriod).toEqual(120000);
88
38
  expect(Config.ResourcesConfig.log).toEqual(noOp);
89
39
  expect(Config.ResourcesConfig.prefilter).toEqual(noOp);
90
40
  expect(Config.ResourcesConfig.track).toEqual(noOp);
91
- expect(Config.ResourcesConfig.queryParamsPropName).toEqual('urlParams');
92
41
 
93
42
  Config.ResourcesConfig.set({
94
43
  cacheGracePeriod: 300000,
95
44
  log: logSpy,
96
45
  prefilter: prefilterSpy,
97
46
  track: trackSpy,
98
- queryParamsPropName: 'queryPs'
99
47
  });
100
48
 
101
49
  expect(Config.ResourcesConfig.cacheGracePeriod).toEqual(300000);
102
50
  expect(Config.ResourcesConfig.log).toEqual(logSpy);
103
51
  expect(Config.ResourcesConfig.prefilter).toEqual(prefilterSpy);
104
52
  expect(Config.ResourcesConfig.track).toEqual(trackSpy);
105
- expect(Config.ResourcesConfig.queryParamsPropName).toEqual('queryPs');
106
53
 
107
54
  Config.ResourcesConfig.set({
108
55
  cacheGracePeriod: 120000,
109
56
  log: noOp,
110
57
  prefilter: noOp,
111
58
  track: noOp,
112
- queryParamsPropName: 'queryParamsPropName'
113
59
  });
114
60
  });
115
61
  });
@@ -1,25 +1,26 @@
1
- import * as sync from '../lib/sync';
2
- import Collection from '../lib/collection';
3
- import Model from '../lib/model';
1
+ import * as sync from "../lib/sync";
2
+ import Collection from "../lib/collection";
3
+ import Model from "../lib/model";
4
+ import { vi } from "vitest";
4
5
 
5
6
  class TestCollection extends Collection {
6
7
  url() {
7
- return '/url';
8
+ return "/url";
8
9
  }
9
10
  }
10
11
 
11
- describe('Events', () => {
12
- describe('a callback is triggered', () => {
13
- var callback = jest.fn(),
14
- collectionCallback = jest.fn(),
15
- model,
16
- collection;
12
+ describe("Events", () => {
13
+ describe("a callback is triggered", () => {
14
+ var callback = vi.fn(),
15
+ collectionCallback = vi.fn(),
16
+ model,
17
+ collection;
17
18
 
18
19
  beforeEach(() => {
19
- model = new Model({id: 'alex'});
20
+ model = new Model({ id: "alex" });
20
21
  model.onUpdate(callback, model);
21
22
 
22
- jest.spyOn(sync, 'default').mockResolvedValue([{}, {}]);
23
+ vi.spyOn(sync, "default").mockResolvedValue([{}, {}]);
23
24
 
24
25
  collection = new TestCollection([model]);
25
26
  collection.onUpdate(collectionCallback, collection);
@@ -31,31 +32,31 @@ describe('Events', () => {
31
32
  sync.default.mockRestore();
32
33
  });
33
34
 
34
- it('when a model updates its attributes', () => {
35
- model.set({noah: 'grant'});
35
+ it("when a model updates its attributes", () => {
36
+ model.set({ noah: "grant" });
36
37
  expect(callback).toHaveBeenCalled();
37
38
  });
38
39
 
39
- it('except when a model\'s attributes have not changed', () => {
40
- model.set({noah: {david: 'grant'}});
40
+ it("except when a model's attributes have not changed", () => {
41
+ model.set({ noah: { david: "grant" } });
41
42
  callback.mockClear();
42
43
 
43
- model.set({noah: {david: 'grant'}});
44
+ model.set({ noah: { david: "grant" } });
44
45
  expect(callback).not.toHaveBeenCalled();
45
46
  });
46
47
 
47
- it('except when a model\'s listener has been removed', () => {
48
+ it("except when a model's listener has been removed", () => {
48
49
  model.offUpdate();
49
- model.set({noah: 'grant'});
50
+ model.set({ noah: "grant" });
50
51
  expect(callback).toHaveBeenCalled();
51
52
 
52
53
  callback.mockClear();
53
54
  model.offUpdate(model);
54
- model.set({noah: 'grant'});
55
+ model.set({ noah: "grant" });
55
56
  expect(callback).not.toHaveBeenCalled();
56
57
  });
57
58
 
58
- it('when a model fetches successfully', async() => {
59
+ it("when a model fetches successfully", async () => {
59
60
  await model.fetch();
60
61
  expect(callback).toHaveBeenCalled();
61
62
 
@@ -66,7 +67,7 @@ describe('Events', () => {
66
67
  expect(callback).not.toHaveBeenCalled();
67
68
  });
68
69
 
69
- it('when a model is successfully deleted', async() => {
70
+ it("when a model is successfully deleted", async () => {
70
71
  var request;
71
72
 
72
73
  request = model.destroy();
@@ -83,19 +84,19 @@ describe('Events', () => {
83
84
  expect(callback).toHaveBeenCalled();
84
85
  callback.mockClear();
85
86
 
86
- await model.destroy({wait: true}).catch(() => false);
87
+ await model.destroy({ wait: true }).catch(() => false);
87
88
  expect(callback).not.toHaveBeenCalled();
88
89
 
89
90
  sync.default.mockResolvedValue([{}, {}]);
90
- request = model.destroy({wait: true});
91
+ request = model.destroy({ wait: true });
91
92
  expect(callback).not.toHaveBeenCalled();
92
93
 
93
94
  await request;
94
95
  expect(callback).toHaveBeenCalledTimes(1);
95
96
  });
96
97
 
97
- it('when a model is successfully saved', async() => {
98
- var request = model.save({noah: 'grant'});
98
+ it("when a model is successfully saved", async () => {
99
+ var request = model.save({ noah: "grant" });
99
100
 
100
101
  // called for initial set
101
102
  expect(callback).toHaveBeenCalled();
@@ -116,48 +117,48 @@ describe('Events', () => {
116
117
  expect(callback).toHaveBeenCalled();
117
118
  callback.mockClear();
118
119
 
119
- request = model.save({zorah: 'fung'}, {wait: true});
120
+ request = model.save({ zorah: "fung" }, { wait: true });
120
121
  expect(callback).not.toHaveBeenCalled();
121
122
 
122
123
  await request.catch(() => false);
123
124
  expect(callback).not.toHaveBeenCalled();
124
125
  sync.default.mockResolvedValue([{}, {}]);
125
126
 
126
- request = model.save({zorah: 'fung'}, {wait: true});
127
+ request = model.save({ zorah: "fung" }, { wait: true });
127
128
  expect(callback).not.toHaveBeenCalled();
128
129
  await request;
129
130
  expect(callback).toHaveBeenCalledTimes(1);
130
131
  });
131
132
 
132
- it('when a model is added to a collection', () => {
133
- collection.add({alex: 'lopatron'});
133
+ it("when a model is added to a collection", () => {
134
+ collection.add({ alex: "lopatron" });
134
135
  expect(collectionCallback).toHaveBeenCalled();
135
136
  });
136
137
 
137
- it('when a model is removed from a collection', () => {
138
+ it("when a model is removed from a collection", () => {
138
139
  collection.remove(model);
139
140
  expect(collectionCallback).toHaveBeenCalled();
140
141
  });
141
142
 
142
- it('when a model on a collection is updated', () => {
143
- model.set({lastName: 'lopatron'});
143
+ it("when a model on a collection is updated", () => {
144
+ model.set({ lastName: "lopatron" });
144
145
  expect(collectionCallback).toHaveBeenCalled();
145
146
  });
146
147
 
147
- it('but not when a model is updated after it has been removed from the collection', () => {
148
+ it("but not when a model is updated after it has been removed from the collection", () => {
148
149
  collection.remove(model);
149
150
  collectionCallback.mockClear();
150
151
 
151
- model.set({lastName: 'lopatron'});
152
+ model.set({ lastName: "lopatron" });
152
153
  expect(collectionCallback).not.toHaveBeenCalled();
153
154
  });
154
155
 
155
- it('when a collection is reset', () => {
156
+ it("when a collection is reset", () => {
156
157
  collection.reset();
157
158
  expect(collectionCallback).toHaveBeenCalled();
158
159
  });
159
160
 
160
- it('when a collection fetches successfully', async() => {
161
+ it("when a collection fetches successfully", async () => {
161
162
  var request = collection.fetch();
162
163
 
163
164
  expect(collectionCallback).not.toHaveBeenCalled();
@@ -172,8 +173,8 @@ describe('Events', () => {
172
173
  expect(collectionCallback).not.toHaveBeenCalled();
173
174
  });
174
175
 
175
- it('when a new model is created on a collection', async() => {
176
- var request = collection.create({alex: 'lopatron'});
176
+ it("when a new model is created on a collection", async () => {
177
+ var request = collection.create({ alex: "lopatron" });
177
178
 
178
179
  // callback gets called first, not later
179
180
  expect(collectionCallback).toHaveBeenCalled();
@@ -183,7 +184,7 @@ describe('Events', () => {
183
184
  expect(collectionCallback).toHaveBeenCalledTimes(1);
184
185
 
185
186
  collectionCallback.mockClear();
186
- request = collection.create({zorah: 'fung'}, {wait: true});
187
+ request = collection.create({ zorah: "fung" }, { wait: true });
187
188
  expect(collectionCallback).not.toHaveBeenCalled();
188
189
 
189
190
  await request;
@@ -192,7 +193,7 @@ describe('Events', () => {
192
193
  });
193
194
  });
194
195
 
195
- it('gracefully removes listeners even if there are none', () => {
196
+ it("gracefully removes listeners even if there are none", () => {
196
197
  var model = new Model();
197
198
 
198
199
  expect(() => model.offUpdate(model)).not.toThrow();
@@ -1,60 +1,61 @@
1
- import Collection from '../lib/collection';
2
- import Model from '../lib/model';
3
- import ModelCache from '../lib/model-cache';
1
+ import Collection from "../lib/collection";
2
+ import Model from "../lib/model";
3
+ import ModelCache from "../lib/model-cache";
4
+ import { vi } from "vitest";
4
5
 
5
6
  const CACHE_WAIT = 150000,
6
- testModel = {};
7
+ testModel = {};
7
8
 
8
- describe('ModelCache', () => {
9
+ describe("ModelCache", () => {
9
10
  beforeEach(() => {
10
- jest.useFakeTimers();
11
- jest.spyOn(ModelCache, 'register');
11
+ vi.useFakeTimers();
12
+ vi.spyOn(ModelCache, "register");
12
13
  });
13
14
 
14
15
  afterEach(() => {
15
16
  ModelCache.__removeAll__();
16
- jest.useRealTimers();
17
+ vi.useRealTimers();
17
18
  ModelCache.register.mockRestore();
18
19
  });
19
20
 
20
- it('gets a model from its cache via a unique key', () => {
21
+ it("gets a model from its cache via a unique key", () => {
21
22
  var model2 = {};
22
23
 
23
- ModelCache.put('foo', testModel, {});
24
- ModelCache.put('bar', model2, {});
25
- expect(ModelCache.get('foo')).toEqual(testModel);
26
- expect(ModelCache.get('bar')).toEqual(model2);
24
+ ModelCache.put("foo", testModel, {});
25
+ ModelCache.put("bar", model2, {});
26
+ expect(ModelCache.get("foo")).toEqual(testModel);
27
+ expect(ModelCache.get("bar")).toEqual(model2);
27
28
  });
28
29
 
29
- describe('when calling \'put\'', () => {
30
- const PUT_KEY = 'putModel',
31
- component = {};
30
+ describe("when calling 'put'", () => {
31
+ const PUT_KEY = "putModel",
32
+ component = {};
32
33
 
33
- it('adds the model to the cache', () => {
34
+ it("adds the model to the cache", () => {
34
35
  ModelCache.put(PUT_KEY, testModel, component);
35
36
  expect(ModelCache.get(PUT_KEY)).toEqual(testModel);
36
37
  });
37
38
 
38
- it('registers the component if a component is passed', () => {
39
+ it("registers the component if a component is passed", () => {
39
40
  ModelCache.put(PUT_KEY, testModel, component);
40
41
  expect(ModelCache.register).toHaveBeenCalledWith(PUT_KEY, component);
41
42
  });
42
43
 
43
- describe('if no component is passed', () => {
44
+ describe("if no component is passed", () => {
44
45
  beforeEach(() => ModelCache.put(PUT_KEY, testModel));
45
46
 
46
- it('does not register one', () => {
47
+ it("does not register one", () => {
47
48
  expect(ModelCache.register).not.toHaveBeenCalled();
48
49
  });
49
50
 
50
- it('schedules the model for removal', () => {
51
+ it("schedules the model for removal", () => {
51
52
  // model should be removed after CACHE_WAIT time
52
53
  expect(ModelCache.get(PUT_KEY)).toBeDefined();
53
- jest.advanceTimersByTime(CACHE_WAIT);
54
+ vi.advanceTimersByTime(CACHE_WAIT);
54
55
  expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
55
56
  });
56
57
 
57
- it('uses a model-specific timeout if available', () => {
58
+ it("uses a model-specific timeout if available", () => {
58
59
  class TimeoutModel extends Model {
59
60
  static cacheTimeout = 3000;
60
61
  }
@@ -63,124 +64,145 @@ describe('ModelCache', () => {
63
64
  static cacheTimeout = CACHE_WAIT + 2000;
64
65
  }
65
66
 
66
- jest.advanceTimersByTime(CACHE_WAIT);
67
+ vi.advanceTimersByTime(CACHE_WAIT);
67
68
  expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
68
69
 
69
70
  ModelCache.put(PUT_KEY, new TimeoutModel());
70
- jest.advanceTimersByTime(2000);
71
+ vi.advanceTimersByTime(2000);
71
72
  expect(ModelCache.get(PUT_KEY)).toBeDefined();
72
- jest.advanceTimersByTime(2000);
73
+ vi.advanceTimersByTime(2000);
73
74
  expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
74
75
 
75
76
  ModelCache.put(PUT_KEY, new TimeoutCollection());
76
- jest.advanceTimersByTime(CACHE_WAIT);
77
+ vi.advanceTimersByTime(CACHE_WAIT);
77
78
  expect(ModelCache.get(PUT_KEY)).toBeDefined();
78
- jest.advanceTimersByTime(2000);
79
+ vi.advanceTimersByTime(2000);
79
80
  expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
80
81
  });
81
82
  });
82
83
  });
83
84
 
84
- describe('registering a component', () => {
85
+ describe("registering a component", () => {
85
86
  const component = {},
86
- component2 = {};
87
+ component2 = {};
87
88
 
88
89
  beforeEach(() => {
89
- ModelCache.put('foo', {}, component);
90
+ ModelCache.put("foo", {}, component);
90
91
  });
91
92
 
92
- it('clears any current cache removal timeouts', () => {
93
+ it("clears any current cache removal timeouts", () => {
93
94
  // if we unregister, we'll see it removed from the cache
94
95
  ModelCache.unregister(component);
95
- jest.advanceTimersByTime(CACHE_WAIT);
96
- expect(ModelCache.get('foo')).not.toBeDefined();
96
+ vi.advanceTimersByTime(CACHE_WAIT);
97
+ expect(ModelCache.get("foo")).not.toBeDefined();
97
98
 
98
99
  // but if we re-register beforehand, we won't
99
- ModelCache.put('foo', {}, component);
100
- expect(ModelCache.get('foo')).toBeDefined();
100
+ ModelCache.put("foo", {}, component);
101
+ expect(ModelCache.get("foo")).toBeDefined();
101
102
  ModelCache.unregister(component);
102
- ModelCache.put('foo', {}, component);
103
- jest.advanceTimersByTime(CACHE_WAIT);
104
- expect(ModelCache.get('foo')).toBeDefined();
103
+ ModelCache.put("foo", {}, component);
104
+ vi.advanceTimersByTime(CACHE_WAIT);
105
+ expect(ModelCache.get("foo")).toBeDefined();
105
106
  });
106
107
 
107
- it('adds the component to the componentManfiest', () => {
108
+ it("adds the component to the componentManfiest", () => {
108
109
  // we don't have access to the component manifest because it's private,
109
110
  // but we can make assertions based on when a model should be cleared
110
111
  // from the cache
111
- ModelCache.register('foo', component);
112
+ ModelCache.register("foo", component);
112
113
  // add new entry
113
- ModelCache.put('foo', {}, component2);
114
+ ModelCache.put("foo", {}, component2);
114
115
  ModelCache.unregister(component2);
115
- jest.advanceTimersByTime(CACHE_WAIT);
116
+ vi.advanceTimersByTime(CACHE_WAIT);
116
117
  // even though the first component didn't add the model, the model should
117
118
  // still exist after component2 was unregistered
118
- expect(ModelCache.get('foo')).toBeDefined();
119
+ expect(ModelCache.get("foo")).toBeDefined();
119
120
  });
120
121
  });
121
122
 
122
- describe('unregistering a component', () => {
123
+ describe("unregistering a component", () => {
123
124
  const component = {};
124
125
 
125
126
  beforeEach(() => {
126
- ModelCache.put('foo', {}, component);
127
- ModelCache.put('bar', {}, component);
128
- ModelCache.put('baz', {}, component);
127
+ ModelCache.put("foo", {}, component);
128
+ ModelCache.put("bar", {}, component);
129
+ ModelCache.put("baz", {}, component);
129
130
  });
130
131
 
131
- it('removes the component from the componentManifest for the given cache keys', () => {
132
- ModelCache.unregister(component, 'foo', 'bar');
133
- jest.advanceTimersByTime(CACHE_WAIT);
134
- expect(ModelCache.get('foo')).not.toBeDefined();
135
- expect(ModelCache.get('bar')).not.toBeDefined();
136
- expect(ModelCache.get('baz')).toBeDefined();
132
+ it("removes the component from the componentManifest for the given cache keys", () => {
133
+ ModelCache.unregister(component, "foo", "bar");
134
+ vi.advanceTimersByTime(CACHE_WAIT);
135
+ expect(ModelCache.get("foo")).not.toBeDefined();
136
+ expect(ModelCache.get("bar")).not.toBeDefined();
137
+ expect(ModelCache.get("baz")).toBeDefined();
137
138
  });
138
139
 
139
- it('removes the component from the entire componentManifest if no cache keys passed', () => {
140
+ it("removes the component from the entire componentManifest if no cache keys passed", () => {
140
141
  ModelCache.unregister(component);
141
- jest.advanceTimersByTime(CACHE_WAIT);
142
- expect(ModelCache.get('foo')).not.toBeDefined();
143
- expect(ModelCache.get('bar')).not.toBeDefined();
144
- expect(ModelCache.get('baz')).not.toBeDefined();
142
+ vi.advanceTimersByTime(CACHE_WAIT);
143
+ expect(ModelCache.get("foo")).not.toBeDefined();
144
+ expect(ModelCache.get("bar")).not.toBeDefined();
145
+ expect(ModelCache.get("baz")).not.toBeDefined();
145
146
  });
146
147
 
147
- it('schedules the model for cache removal if it no longer has components registered', () => {
148
+ it("schedules the model for cache removal if it no longer has components registered", () => {
148
149
  // let's add another component to foo and to baz
149
- ModelCache.put('foo', {}, {});
150
- ModelCache.put('bar', {}, {});
150
+ ModelCache.put("foo", {}, {});
151
+ ModelCache.put("bar", {}, {});
151
152
 
152
153
  ModelCache.unregister(component);
153
- jest.advanceTimersByTime(CACHE_WAIT);
154
+ vi.advanceTimersByTime(CACHE_WAIT);
154
155
 
155
- expect(ModelCache.get('foo')).toBeDefined();
156
- expect(ModelCache.get('bar')).toBeDefined();
156
+ expect(ModelCache.get("foo")).toBeDefined();
157
+ expect(ModelCache.get("bar")).toBeDefined();
157
158
  // only baz will get removed, since the others have other components
158
159
  // still attached
159
- expect(ModelCache.get('baz')).not.toBeDefined();
160
+ expect(ModelCache.get("baz")).not.toBeDefined();
160
161
  ModelCache.unregister({});
161
162
  });
162
163
  });
163
164
 
164
- it('when calling \'remove\' removes the model from the cache immediately', () => {
165
+ it("when calling 'remove' removes the model from the cache immediately", () => {
165
166
  const component = {};
166
167
 
167
168
  // let's register foo to three components
168
- ModelCache.put('foo', {}, {});
169
- ModelCache.put('foo', {}, {});
170
- ModelCache.put('foo', {}, {});
169
+ ModelCache.put("foo", {}, {});
170
+ ModelCache.put("foo", {}, {});
171
+ ModelCache.put("foo", {}, {});
171
172
 
172
- expect(ModelCache.get('foo')).toBeDefined();
173
- ModelCache.remove('foo');
174
- expect(ModelCache.get('foo')).not.toBeDefined();
173
+ expect(ModelCache.get("foo")).toBeDefined();
174
+ ModelCache.remove("foo");
175
+ expect(ModelCache.get("foo")).not.toBeDefined();
175
176
 
176
- ModelCache.put('foo', {}, component);
177
+ ModelCache.put("foo", {}, component);
177
178
  ModelCache.unregister(component);
178
179
 
179
180
  // still defined because of the timeout
180
- expect(ModelCache.get('foo')).toBeDefined();
181
+ expect(ModelCache.get("foo")).toBeDefined();
181
182
 
182
- ModelCache.remove('foo');
183
+ ModelCache.remove("foo");
183
184
  // now immediately gone
184
- expect(ModelCache.get('foo')).not.toBeDefined();
185
+ expect(ModelCache.get("foo")).not.toBeDefined();
186
+ });
187
+
188
+ it("when calling 'removeAllWithModel' removes all models of a specific key", () => {
189
+ const cacheKeys = [
190
+ "user~userId=zorah",
191
+ "user~source=hbase_userId=noah",
192
+ "user",
193
+ "users",
194
+ "users~key=value",
195
+ "decisions",
196
+ ];
197
+
198
+ cacheKeys.forEach((key) => ModelCache.put(key, {}, {}));
199
+
200
+ ModelCache.removeAllWithModel("user");
201
+ expect(ModelCache.get("user")).not.toBeDefined();
202
+ expect(ModelCache.get("user~userId=zorah")).not.toBeDefined();
203
+ expect(ModelCache.get("user~source=hbase_userId=noah")).not.toBeDefined();
204
+ expect(ModelCache.get("users")).toBeDefined();
205
+ expect(ModelCache.get("users~key=value")).toBeDefined();
206
+ expect(ModelCache.get("decisions")).toBeDefined();
185
207
  });
186
208
  });