|
@@ -7,107 +7,41 @@ describe("basic-2", () => {
|
|
// Configure the client to use the local cluster.
|
|
// Configure the client to use the local cluster.
|
|
anchor.setProvider(provider);
|
|
anchor.setProvider(provider);
|
|
|
|
|
|
- // Author for the tests.
|
|
|
|
- const author = new anchor.web3.Account();
|
|
|
|
|
|
+ // Counter for the tests.
|
|
|
|
+ const counter = new anchor.web3.Account();
|
|
|
|
|
|
// Program for the tests.
|
|
// Program for the tests.
|
|
const program = anchor.workspace.Basic2;
|
|
const program = anchor.workspace.Basic2;
|
|
|
|
|
|
- it("Creates an author", async () => {
|
|
|
|
- await program.rpc.createAuthor(provider.wallet.publicKey, "Ghost", {
|
|
|
|
|
|
+ it("Creates a counter", async () => {
|
|
|
|
+ await program.rpc.create(provider.wallet.publicKey, {
|
|
accounts: {
|
|
accounts: {
|
|
- author: author.publicKey,
|
|
|
|
|
|
+ counter: counter.publicKey,
|
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
},
|
|
},
|
|
- signers: [author],
|
|
|
|
- instructions: [
|
|
|
|
- anchor.web3.SystemProgram.createAccount({
|
|
|
|
- fromPubkey: provider.wallet.publicKey,
|
|
|
|
- newAccountPubkey: author.publicKey,
|
|
|
|
- space: 8 + 1000,
|
|
|
|
- lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
|
|
|
- 8 + 1000
|
|
|
|
- ),
|
|
|
|
- programId: program.programId,
|
|
|
|
- }),
|
|
|
|
- ],
|
|
|
|
|
|
+ signers: [counter],
|
|
|
|
+ instructions: [
|
|
|
|
+ await program.account.counter.createInstruction(counter),
|
|
|
|
+ ],
|
|
});
|
|
});
|
|
|
|
|
|
- let authorAccount = await program.account.author(author.publicKey);
|
|
|
|
|
|
+ let counterAccount = await program.account.counter(counter.publicKey);
|
|
|
|
|
|
- assert.ok(authorAccount.authority.equals(provider.wallet.publicKey));
|
|
|
|
- assert.ok(authorAccount.name === "Ghost");
|
|
|
|
|
|
+ assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
|
|
|
|
+ assert.ok(counterAccount.count.toNumber() === 0);
|
|
});
|
|
});
|
|
|
|
|
|
- it("Updates an author", async () => {
|
|
|
|
- await program.rpc.updateAuthor("Updated author", {
|
|
|
|
|
|
+ it("Updates a counter", async () => {
|
|
|
|
+ await program.rpc.increment({
|
|
accounts: {
|
|
accounts: {
|
|
- author: author.publicKey,
|
|
|
|
|
|
+ counter: counter.publicKey,
|
|
authority: provider.wallet.publicKey,
|
|
authority: provider.wallet.publicKey,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
|
|
- authorAccount = await program.account.author(author.publicKey);
|
|
|
|
|
|
+ counterAccount = await program.account.counter(counter.publicKey);
|
|
|
|
|
|
- assert.ok(authorAccount.authority.equals(provider.wallet.publicKey));
|
|
|
|
- assert.ok(authorAccount.name === "Updated author");
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- // Book params to use accross tests.
|
|
|
|
- const book = new anchor.web3.Account();
|
|
|
|
- const pages = [
|
|
|
|
- {
|
|
|
|
- content: "first page",
|
|
|
|
- footnote: "first footnote",
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- content: "second page",
|
|
|
|
- footnote: "second footnote",
|
|
|
|
- },
|
|
|
|
- ];
|
|
|
|
-
|
|
|
|
- it("Creates a book", async () => {
|
|
|
|
- await program.rpc.createBook("Book title", pages, {
|
|
|
|
- accounts: {
|
|
|
|
- authority: provider.wallet.publicKey,
|
|
|
|
- author: author.publicKey,
|
|
|
|
- book: book.publicKey,
|
|
|
|
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
|
- },
|
|
|
|
- signers: [book],
|
|
|
|
- instructions: [
|
|
|
|
- anchor.web3.SystemProgram.createAccount({
|
|
|
|
- fromPubkey: provider.wallet.publicKey,
|
|
|
|
- newAccountPubkey: book.publicKey,
|
|
|
|
- space: 8 + 1000,
|
|
|
|
- lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
|
|
|
- 8 + 1000
|
|
|
|
- ),
|
|
|
|
- programId: program.programId,
|
|
|
|
- }),
|
|
|
|
- ],
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- const bookAccount = await program.account.book(book.publicKey);
|
|
|
|
-
|
|
|
|
- assert.ok(bookAccount.author.equals(author.publicKey));
|
|
|
|
- assert.ok(bookAccount.title === "Book title");
|
|
|
|
- assert.deepEqual(bookAccount.pages, pages);
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- it("Updates a book", async () => {
|
|
|
|
- await program.rpc.updateBook("New book title", null, {
|
|
|
|
- accounts: {
|
|
|
|
- authority: provider.wallet.publicKey,
|
|
|
|
- author: author.publicKey,
|
|
|
|
- book: book.publicKey,
|
|
|
|
- },
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- const bookAccount = await program.account.book(book.publicKey);
|
|
|
|
-
|
|
|
|
- assert.ok(bookAccount.author.equals(author.publicKey));
|
|
|
|
- assert.ok(bookAccount.title === "New book title");
|
|
|
|
- assert.deepEqual(bookAccount.pages, pages);
|
|
|
|
|
|
+ assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
|
|
|
|
+ assert.ok(counterAccount.count.toNumber() == 1);
|
|
});
|
|
});
|
|
});
|
|
});
|