/ test / memorystore.test.ts
memorystore.test.ts
 1  import { RandomStoreOptions, MemoryRandomStore } from "../src";
 2  
 3  describe("memory store", () => {
 4    test("can't add the same string twice", () => {
 5      const store = new MemoryRandomStore();
 6      expect(store.addRandom("random string")).toBeTruthy()
 7      expect(store.addRandom("random string")).toBeFalsy()
 8    })
 9  
10    test("can't validate the same string twice if invalidate is set", () => {
11      const store = new MemoryRandomStore();
12      expect(store.addRandom("random string")).toBeTruthy()
13      expect(store.validateRandom("random string")).toBeTruthy()
14      expect(store.validateRandom("random string")).toBeFalsy()
15    })
16  
17    test("can validate the same string twice if invalidate is set to false", () => {
18      const store = new MemoryRandomStore();
19      expect(store.addRandom("random string")).toBeTruthy()
20      expect(store.validateRandom("random string", false)).toBeTruthy()
21      expect(store.validateRandom("random string")).toBeTruthy()
22      expect(store.validateRandom("random string")).toBeFalsy()
23    })
24  
25    test("can't use a string after it expires", (done) => {
26      const options: RandomStoreOptions = {
27        expiryTimeSeconds: 1
28      }
29      const store = new MemoryRandomStore(options);
30      expect(store.addRandom("random string")).toBeTruthy();
31      setTimeout(() => {
32        try {
33          expect(store.validateRandom("random string")).toBeFalsy();
34          done();
35        } catch (error) {
36          done(error);
37        }
38      }, 1000 * (options.expiryTimeSeconds! + 1))
39    })
40  })