UNPKG

4.06 kBJavaScriptView Raw
1/**
2 * Created by Andy Likuski on 2018.12.28
3 * Copyright (c) 2018 Andy Likuski
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 *
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 */
11
12import {memoized, memoizedWith} from './memoizeHelpers';
13import * as R from 'ramda';
14import NamedTupleMap from 'namedtuplemap';
15
16describe('memoizeHelpers', () => {
17 test('NamedTupleMap', async () => {
18 const cache = new NamedTupleMap();
19 const keyMap = {crazy: 8888, mother: 'hubbard'};
20 const value = {any: 'thing'};
21 cache.set(keyMap, value);
22 const res = cache.get({crazy: 8888, mother: 'hubbard'});
23 expect(res).toEqual(value);
24 });
25
26 test('memoized', () => {
27 let i = 0;
28 // The func increments i in order to prove that the function is memoized and only called when arguments change
29 const func = (apple, orange, universe) => {
30 return {apple, orange, i: i++};
31 };
32 const memoizedFunc = memoized(func);
33 const crazy = {crazy: 888};
34 const now = memoizedFunc(1, 2, crazy);
35 // This should hit the cache and not call the function
36 const later = memoizedFunc(1, 2, crazy);
37 expect(later).toEqual(now);
38 // This should call the function anew
39 const muchLater = memoizedFunc(1, 2, {crazy: 889});
40 expect(muchLater).toEqual(R.merge(now, {i: 1}));
41
42 const deepTrouble = snooky => {
43 i++;
44 return R.view(R.lensPath('a', 'b', 'c', 'd'), snooky + i);
45 };
46 const deep = memoized(deepTrouble);
47 expect(deep({a: {b: {c: {d: 5}}}})).toEqual((deep({a: {b: {c: {d: 5}}}})));
48 });
49
50 test('memoizedWith', () => {
51 let i = 0;
52 // The func increments i in order to prove that the function is memoized and only called when arguments change
53 const func = (apple, orange, universe) => {
54 i++;
55 return {
56 apple,
57 orange,
58 i: i + universe.crazy,
59 j: i + R.or(0, R.view(R.lensPath(['alot', 'of', 'stuff', 'that', 'equals']), universe))
60 };
61 };
62
63 const memoizedFunc = memoizedWith(
64 (apple, orange, universe) => [apple, orange, R.omit(['alot'], universe)],
65 func
66 );
67
68 const crazy = {crazy: 888, alot: {of: {stuff: {that: {equals: 5}}}}};
69 // Separate args to make sure currying works
70 const now = memoizedFunc(1)(2)(crazy);
71 // j should have the results of even though we didn't use alot...it for the memoize
72 expect(now.j).toEqual(1 + 5);
73 // This should hit the cache and not call the function
74 const later = memoizedFunc(1, 2, crazy);
75 expect(later).toEqual(now);
76
77 // Even though this value would give a different result, we don't consider alot... in the memoize so a cached
78 // result is returned
79 const offthehizzle = {crazy: 888, alot: {of: {stuff: {that: {equals: 6}}}}};
80 const muchLater = memoizedFunc(1, 2, offthehizzle);
81 expect(muchLater).toEqual(now);
82
83 // When we change crazy we get a cash miss and everything updates
84 const offtheshizzle = {crazy: 889, alot: {of: {stuff: {that: {equals: 6}}}}};
85 const mostLater = memoizedFunc(1, 2, offtheshizzle);
86 expect(mostLater).toEqual(R.merge(now, {i: 2 + 889, j: 2 + 6}));
87 });
88});