program-common.spec.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import BN from "bn.js";
  2. import bs58 from "bs58";
  3. import { PublicKey } from "@solana/web3.js";
  4. import { translateAddress } from "../src/program/common";
  5. describe("program/common", () => {
  6. describe("translateAddress", () => {
  7. it("should accept a valid string address", () => {
  8. const address = "11111111111111111111111111111111";
  9. const func = () => translateAddress(address);
  10. expect(func).not.toThrow();
  11. const output = func();
  12. expect(output).toBeInstanceOf(PublicKey);
  13. expect(new PublicKey(address).equals(output)).toBeTruthy();
  14. });
  15. it("should accept a PublicKey address", () => {
  16. const publicKey = new PublicKey("11111111111111111111111111111111");
  17. const func = () => translateAddress(publicKey);
  18. expect(func).not.toThrow();
  19. const output = func();
  20. expect(output).toBeInstanceOf(PublicKey);
  21. expect(new PublicKey(publicKey).equals(output)).toBe(true);
  22. });
  23. it("should accept an object with a PublicKey shape { _bn }", () => {
  24. const obj = ({
  25. _bn: new BN(bs58.decode("11111111111111111111111111111111")),
  26. } as any) as PublicKey;
  27. const func = () => translateAddress(obj);
  28. expect(func).not.toThrow();
  29. const output = func();
  30. expect(output).toBeInstanceOf(PublicKey);
  31. expect(new PublicKey(obj).equals(output)).toBe(true);
  32. });
  33. it("should not accept an invalid string address", () => {
  34. const invalid = "invalid";
  35. const func = () => translateAddress(invalid);
  36. expect(func).toThrow();
  37. });
  38. it("should not accept an invalid object", () => {
  39. const invalid = {} as PublicKey;
  40. const func = () => translateAddress(invalid);
  41. expect(func).toThrow();
  42. });
  43. });
  44. });