realm.spec.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, it } from "@jest/globals";
  2. import { Realm } from "../../src/models/realm";
  3. import { Client } from "../../src/models/client";
  4. describe("Realm", () => {
  5. describe("#generateClientId", () => {
  6. it("should generate a 36-character UUID, or return function value", () => {
  7. const realm = new Realm();
  8. expect(realm.generateClientId().length).toBe(36);
  9. expect(realm.generateClientId(() => "abcd")).toBe("abcd");
  10. });
  11. });
  12. describe("#setClient", () => {
  13. it("should add client to realm", () => {
  14. const realm = new Realm();
  15. const client = new Client({ id: "id", token: "" });
  16. realm.setClient(client, "id");
  17. expect(realm.getClientsIds()).toEqual(["id"]);
  18. });
  19. });
  20. describe("#removeClientById", () => {
  21. it("should remove client from realm", () => {
  22. const realm = new Realm();
  23. const client = new Client({ id: "id", token: "" });
  24. realm.setClient(client, "id");
  25. realm.removeClientById("id");
  26. expect(realm.getClientById("id")).toBeUndefined();
  27. });
  28. });
  29. describe("#getClientsIds", () => {
  30. it("should reflects on add/remove childs", () => {
  31. const realm = new Realm();
  32. const client = new Client({ id: "id", token: "" });
  33. realm.setClient(client, "id");
  34. expect(realm.getClientsIds()).toEqual(["id"]);
  35. expect(realm.getClientById("id")).toBe(client);
  36. realm.removeClientById("id");
  37. expect(realm.getClientsIds()).toEqual([]);
  38. });
  39. });
  40. });