« get me outta code hell

cacheable-object.js « test - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/cacheable-object.js
blob: 664a648b2dc30b2e81a9a91558541abfc8b17c9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import test from 'tape';

import CacheableObject from '../src/data/cacheable-object.js';

// Utility

function newCacheableObject(PD) {
  return new (class extends CacheableObject {
    static propertyDescriptors = PD;
  });
}

// Tests

test(`CacheableObject simple separate update & expose`, t => {
  const obj = newCacheableObject({
    number: {
      flags: {
        update: true
      }
    },

    timesTwo: {
      flags: {
        expose: true
      },

      expose: {
        dependencies: ['number'],
        compute: ({ number }) => number * 2
      }
    }
  });

  t.plan(1);
  obj.number = 5;
  t.equal(obj.timesTwo, 10);
});

test(`CacheableObject basic cache behavior`, t => {
  let computeCount = 0;

  const obj = newCacheableObject({
    string: {
      flags: {
        update: true
      }
    },

    karkat: {
      flags: {
        expose: true
      },

      expose: {
        dependencies: ['string'],
        compute: ({ string }) => {
          computeCount++;
          return string.toUpperCase();
        }
      }
    }
  });

  t.plan(8);

  t.is(computeCount, 0);

  obj.string = 'hello world';
  t.is(computeCount, 0);

  obj.karkat;
  t.is(computeCount, 1);

  obj.karkat;
  t.is(computeCount, 1);

  obj.string = 'testing once again';
  t.is(computeCount, 1);

  obj.karkat;
  t.is(computeCount, 2);

  obj.string = 'testing once again';
  t.is(computeCount, 2);

  obj.karkat;
  t.is(computeCount, 2);
});

test(`CacheableObject combined update & expose (no transform)`, t => {
  const obj = newCacheableObject({
    directory: {
      flags: {
        update: true,
        expose: true
      }
    }
  });

  t.plan(2);

  t.directory = 'the-world-revolving';
  t.is(t.directory, 'the-world-revolving');

  t.directory = 'chaos-king';
  t.is(t.directory, 'chaos-king');
});

test(`CacheableObject combined update & expose (basic transform)`, t => {
  const obj = newCacheableObject({
    getsRepeated: {
      flags: {
        update: true,
        expose: true
      },

      expose: {
        transform: value => value.repeat(2)
      }
    }
  });

  t.plan(1);

  obj.getsRepeated = 'dog';
  t.is(obj.getsRepeated, 'dogdog');
});

test(`CacheableObject combined update & expose (transform with dependency)`, t => {
  const obj = newCacheableObject({
    customRepeat: {
      flags: {
        update: true,
        expose: true
      },

      expose: {
        dependencies: ['times'],
        transform: (value, { times }) => value.repeat(times)
      }
    },

    times: {
      flags: {
        update: true
      }
    }
  });

  t.plan(3);

  obj.customRepeat = 'dog';
  obj.times = 1;
  t.is(obj.customRepeat, 'dog');

  obj.times = 5;
  t.is(obj.customRepeat, 'dogdogdogdogdog');

  obj.customRepeat = 'cat';
  t.is(obj.customRepeat, 'catcatcatcatcat');
});

test(`CacheableObject validate on update`, t => {
  const mockError = new TypeError(`Expected a string, not ${typeof value}`);

  const obj = newCacheableObject({
    directory: {
      flags: {
        update: true,
        expose: true
      },

      update: {
        validate: value => {
          if (typeof value !== 'string') {
            throw mockError;
          }
          return true;
        }
      }
    },

    date: {
      flags: {
        update: true,
        expose: true
      },

      update: {
        validate: value => (value instanceof Date)
      }
    }
  });

  let thrownError;
  t.plan(6);

  obj.directory = 'megalovania';
  t.is(obj.directory, 'megalovania');

  try {
    obj.directory = 25;
  } catch (err) {
    thrownError = err;
  }

  t.is(thrownError, mockError);
  t.is(obj.directory, 'megalovania');

  const date = new Date(`25 December 2009`);

  obj.date = date;
  t.is(obj.date, date);

  try {
    obj.date = `TWELFTH PERIGEE'S EVE`;
  } catch (err) {
    thrownError = err;
  }

  t.is(thrownError?.constructor, TypeError);
  t.is(obj.date, date);
});

test(`CacheableObject default update property value`, t => {
  const obj = newCacheableObject({
    fruit: {
      flags: {
        update: true,
        expose: true
      },

      update: {
        default: 'potassium'
      }
    }
  });

  t.plan(1);
  t.is(obj.fruit, 'potassium');
});

test(`CacheableObject default property throws if invalid`, t => {
  const mockError = new TypeError(`Expected a string, not ${typeof value}`);

  t.plan(1);

  let thrownError;

  try {
    newCacheableObject({
      string: {
        flags: {
          update: true
        },

        update: {
          default: 123,
          validate: value => {
            if (typeof value !== 'string') {
              throw mockError;
            }
            return true;
          }
        }
      }
    });
  } catch (err) {
    thrownError = err;
  }

  t.is(thrownError, mockError);
});