|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +const Quellify_1 = require("../src/Quellify"); |
| 13 | +const defaultCostOptions = { |
| 14 | + maxCost: 5000, |
| 15 | + mutationCost: 5, |
| 16 | + objectCost: 2, |
| 17 | + scalarCost: 1, |
| 18 | + depthCostFactor: 1.5, |
| 19 | + maxDepth: 10, |
| 20 | + ipRate: 3 |
| 21 | +}; |
| 22 | +// Command to run jest tests: |
| 23 | +// npx jest client/src/quell-client/client-tests/__tests__/quellify.test.ts |
| 24 | +describe('Quellify', () => { |
| 25 | + beforeEach(() => { |
| 26 | + // Clear the client cache before each test |
| 27 | + (0, Quellify_1.clearCache)(); |
| 28 | + }); |
| 29 | + it('checks that caching queries is working correctly', () => __awaiter(void 0, void 0, void 0, function* () { |
| 30 | + const endPoint = 'http://localhost:3000/api/graphql'; |
| 31 | + const query = 'query { artist(name: "Frank Ocean") { id name albums { id name } } }'; |
| 32 | + const costOptions = defaultCostOptions; |
| 33 | + const [data, foundInCache] = yield (0, Quellify_1.Quellify)(endPoint, query, costOptions); |
| 34 | + // Assertion: the data should not be found in the cache |
| 35 | + expect(foundInCache).toBe(false); |
| 36 | + // Invoke Quellify on query again |
| 37 | + const [cachedData, updatedCache] = yield (0, Quellify_1.Quellify)(endPoint, query, costOptions); |
| 38 | + // Assertion: Cached data should be the same as the original query |
| 39 | + expect(cachedData).toBe(data); |
| 40 | + // Assertion: The boolean should return true if it is found in the cache |
| 41 | + expect(updatedCache).toEqual(true); |
| 42 | + })); |
| 43 | + it('should update the cache for edit mutation queries', () => __awaiter(void 0, void 0, void 0, function* () { |
| 44 | + const endPoint = 'http://localhost:3000/api/graphql'; |
| 45 | + const addQuery = 'mutation { addCity(name: "San Diego", country: "United States") { id name } }'; |
| 46 | + const costOptions = defaultCostOptions; |
| 47 | + // Perform add mutation query to the cache |
| 48 | + const [addMutationData, addMutationfoundInCache] = yield (0, Quellify_1.Quellify)(endPoint, addQuery, costOptions); |
| 49 | + // Get the cityId on the mutation query |
| 50 | + const cityId = addMutationData.addCity.id; |
| 51 | + const city = "Las Vegas"; |
| 52 | + // Perform edit mutation on query to update the name |
| 53 | + const editQuery = `mutation { editCity(id: "${cityId}", name: "${city}", country: "United States") { id name } }`; |
| 54 | + const [editMutationData, editMutationDataFoundInCache] = yield (0, Quellify_1.Quellify)(endPoint, editQuery, costOptions); |
| 55 | + //Assertion: The first mutation query name should be updated by the second edit mutation |
| 56 | + expect(addMutationData.name).toEqual(editMutationData.name); |
| 57 | + })); |
| 58 | + it('should delete an item from the server and invalidate the cache', () => __awaiter(void 0, void 0, void 0, function* () { |
| 59 | + const endPoint = 'http://localhost:3000/api/graphql'; |
| 60 | + const addQuery = 'mutation { addCity(name: "Irvine", country: "United States") { id name } }'; |
| 61 | + const costOptions = defaultCostOptions; |
| 62 | + // Perform add mutation query to the server |
| 63 | + const [addMutationData, addMutationfoundInCache] = yield (0, Quellify_1.Quellify)(endPoint, addQuery, costOptions); |
| 64 | + // Get the cityId on the mutation query |
| 65 | + const cityId = addMutationData.addCity.id; |
| 66 | + // Perform a delete mutation on the city |
| 67 | + const deleteQuery = `mutation { deleteCity(id: "${cityId}") { id name } }`; |
| 68 | + const [deleteMutationData, deleteMutationDataFoundInCache] = yield (0, Quellify_1.Quellify)(endPoint, deleteQuery, costOptions); |
| 69 | + //Assertion: The item should be removed from the cache |
| 70 | + expect(deleteMutationDataFoundInCache).toBe(false); |
| 71 | + })); |
| 72 | + it('should evict the LRU item from cache if cache size is exceeded', () => __awaiter(void 0, void 0, void 0, function* () { |
| 73 | + const endPoint = 'http://localhost:3000/api/graphql'; |
| 74 | + const costOptions = defaultCostOptions; |
| 75 | + const query1 = 'query { artist(name: "Frank Ocean") { id name albums { id name } } }'; |
| 76 | + const query2 = 'query { country(name: "United States") { id name cities { id name attractions { id name } } } }'; |
| 77 | + const query3 = 'mutation { addCity(name: "San Diego", country: "United States") { id name } }'; |
| 78 | + // Invoke Quellify on each query to add to cache |
| 79 | + yield (0, Quellify_1.Quellify)(endPoint, query1, costOptions); |
| 80 | + yield (0, Quellify_1.Quellify)(endPoint, query2, costOptions); |
| 81 | + // Assertion: lruCache should contain the queries |
| 82 | + expect(Quellify_1.lruCache.has(query1)).toBe(true); |
| 83 | + expect(Quellify_1.lruCache.has(query2)).toBe(true); |
| 84 | + // Invoke Quellify again on third query to exceed max cache size |
| 85 | + yield (0, Quellify_1.Quellify)(endPoint, query3, costOptions); |
| 86 | + // Assertion: lruCache should evict the LRU item |
| 87 | + expect(Quellify_1.lruCache.has(query1)).toBe(false); |
| 88 | + // Assertion: lruCache should still contain the most recently used items |
| 89 | + expect(Quellify_1.lruCache.has(query2)).toBe(true); |
| 90 | + expect(Quellify_1.lruCache.has(query3)).toBe(true); |
| 91 | + })); |
| 92 | +}); |
0 commit comments