basic-2.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const assert = require("assert");
  2. const anchor = require('@project-serum/anchor');
  3. describe("basic-2", () => {
  4. const provider = anchor.Provider.local();
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(provider);
  7. // Author for the tests.
  8. const author = new anchor.web3.Account();
  9. // Program for the tests.
  10. const program = anchor.workspace.Basic2;
  11. it("Creates an author", async () => {
  12. await program.rpc.createAuthor(provider.wallet.publicKey, "Ghost", {
  13. accounts: {
  14. author: author.publicKey,
  15. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  16. },
  17. signers: [author],
  18. instructions: [
  19. anchor.web3.SystemProgram.createAccount({
  20. fromPubkey: provider.wallet.publicKey,
  21. newAccountPubkey: author.publicKey,
  22. space: 8 + 1000,
  23. lamports: await provider.connection.getMinimumBalanceForRentExemption(
  24. 8 + 1000
  25. ),
  26. programId: program.programId,
  27. }),
  28. ],
  29. });
  30. let authorAccount = await program.account.author(author.publicKey);
  31. assert.ok(authorAccount.authority.equals(provider.wallet.publicKey));
  32. assert.ok(authorAccount.name === "Ghost");
  33. });
  34. it("Updates an author", async () => {
  35. await program.rpc.updateAuthor("Updated author", {
  36. accounts: {
  37. author: author.publicKey,
  38. authority: provider.wallet.publicKey,
  39. },
  40. });
  41. authorAccount = await program.account.author(author.publicKey);
  42. assert.ok(authorAccount.authority.equals(provider.wallet.publicKey));
  43. assert.ok(authorAccount.name === "Updated author");
  44. });
  45. // Book params to use accross tests.
  46. const book = new anchor.web3.Account();
  47. const pages = [
  48. {
  49. content: "first page",
  50. footnote: "first footnote",
  51. },
  52. {
  53. content: "second page",
  54. footnote: "second footnote",
  55. },
  56. ];
  57. it("Creates a book", async () => {
  58. await program.rpc.createBook("Book title", pages, {
  59. accounts: {
  60. authority: provider.wallet.publicKey,
  61. author: author.publicKey,
  62. book: book.publicKey,
  63. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  64. },
  65. signers: [book],
  66. instructions: [
  67. anchor.web3.SystemProgram.createAccount({
  68. fromPubkey: provider.wallet.publicKey,
  69. newAccountPubkey: book.publicKey,
  70. space: 8 + 1000,
  71. lamports: await provider.connection.getMinimumBalanceForRentExemption(
  72. 8 + 1000
  73. ),
  74. programId: program.programId,
  75. }),
  76. ],
  77. });
  78. const bookAccount = await program.account.book(book.publicKey);
  79. assert.ok(bookAccount.author.equals(author.publicKey));
  80. assert.ok(bookAccount.title === "Book title");
  81. assert.deepEqual(bookAccount.pages, pages);
  82. });
  83. it("Updates a book", async () => {
  84. await program.rpc.updateBook("New book title", null, {
  85. accounts: {
  86. authority: provider.wallet.publicKey,
  87. author: author.publicKey,
  88. book: book.publicKey,
  89. },
  90. });
  91. const bookAccount = await program.account.book(book.publicKey);
  92. assert.ok(bookAccount.author.equals(author.publicKey));
  93. assert.ok(bookAccount.title === "New book title");
  94. assert.deepEqual(bookAccount.pages, pages);
  95. });
  96. });