realtime-cache.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { readFileSync } from "fs";
  2. import path from "path";
  3. describe("Realtime movies cache update logic", () => {
  4. const src = readFileSync(
  5. path.resolve(__dirname, "../../hooks/use-realtime-movies.ts"),
  6. "utf-8",
  7. );
  8. it("handles INSERT events with duplicate guard", () => {
  9. expect(src).toContain("INSERT");
  10. // Verify duplicate check exists across pages to prevent optimistic update collisions
  11. expect(src).toMatch(/pages\.some.*page.*some.*\.id\s*===\s*newMovie\.id/);
  12. });
  13. it("handles UPDATE events with map replacement", () => {
  14. expect(src).toContain("UPDATE");
  15. // Verify map-based replacement across pages
  16. expect(src).toMatch(/pages\.map\(/);
  17. });
  18. it("handles DELETE events with filter removal", () => {
  19. expect(src).toContain("DELETE");
  20. // Verify filter-based removal across pages
  21. expect(src).toMatch(/page\.filter\(/);
  22. });
  23. it("guards against null old data in setQueryData callbacks", () => {
  24. // Each callback should handle the case where old cache data is undefined/null
  25. const setQueryDataBlocks = src.match(/setQueryData.*?\(.*?old.*?\)/gs);
  26. expect(setQueryDataBlocks).not.toBeNull();
  27. expect(src).toMatch(/if\s*\(\s*!old\s*\)/);
  28. });
  29. it("uses moviesQueryKey helper for consistent key generation", () => {
  30. expect(src).toMatch(/function\s+moviesQueryKey/);
  31. // Verify the helper is used in the callback
  32. expect(src).toMatch(/const\s+key\s*=\s*moviesQueryKey\(/);
  33. });
  34. });