basic-2.js 3.3 KB

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