program-common.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import BN from "bn.js";
  2. import bs58 from "bs58";
  3. import { PublicKey } from "@solana/web3.js";
  4. import NodeWallet from "../src/nodewallet";
  5. import { translateAddress } from "../src/program/common";
  6. describe("program/common", () => {
  7. describe("translateAddress", () => {
  8. it("should accept a valid string address", () => {
  9. const address = "11111111111111111111111111111111";
  10. const func = () => translateAddress(address);
  11. expect(func).not.toThrow();
  12. const output = func();
  13. expect(output).toBeInstanceOf(PublicKey);
  14. expect(new PublicKey(address).equals(output)).toBeTruthy();
  15. });
  16. it("should accept a PublicKey address", () => {
  17. const publicKey = new PublicKey("11111111111111111111111111111111");
  18. const func = () => translateAddress(publicKey);
  19. expect(func).not.toThrow();
  20. const output = func();
  21. expect(output).toBeInstanceOf(PublicKey);
  22. expect(new PublicKey(publicKey).equals(output)).toBe(true);
  23. });
  24. it("should accept an object with a PublicKey shape { _bn }", () => {
  25. const obj = {
  26. _bn: new BN(bs58.decode("11111111111111111111111111111111")),
  27. } as any as PublicKey;
  28. const func = () => translateAddress(obj);
  29. expect(func).not.toThrow();
  30. const output = func();
  31. expect(output).toBeInstanceOf(PublicKey);
  32. expect(new PublicKey(obj).equals(output)).toBe(true);
  33. });
  34. it("should not accept an invalid string address", () => {
  35. const invalid = "invalid";
  36. const func = () => translateAddress(invalid);
  37. expect(func).toThrow();
  38. });
  39. it("should not accept an invalid object", () => {
  40. const invalid = {} as PublicKey;
  41. const func = () => translateAddress(invalid);
  42. expect(func).toThrow();
  43. });
  44. });
  45. describe("NodeWallet", () => {
  46. it("should throw an error when ANCHOR_WALLET is unset", () => {
  47. const oldValue = process.env.ANCHOR_WALLET;
  48. delete process.env.ANCHOR_WALLET;
  49. expect(() => NodeWallet.local()).toThrowError(
  50. "expected environment variable `ANCHOR_WALLET` is not set."
  51. );
  52. process.env.ANCHOR_WALLET = oldValue;
  53. });
  54. });
  55. });