Integration tests standar
You should use BDD for integration testing and e2e.
this means that the tests are focused on the business rules, in the expected behavior of the app. So, to do this we follow a standard.
Standar
We are using Cucumber - Gherkin Standard to document all the features that the app needs to have.
e.g
We have one feature here with two different scenarios: Feature: A Holder of an NFT collection wants to see the metadata of his NFT after minting it. Scenario 1: before start staking of the token Scenario 2: After start staking of the token. Because of the business we have different logic for staked NFTS.
describe('feature: As a Lil Hero holder I want to see the metadata of my NFTs', () => {
const baseUrl = 'https://unrevealedCollection.com/';
const baseUrlRevealed = 'https://revealedCollection.com/';
describe(`Scenario: I already minted NFT#1, but I don't start staking yet`, () => {
let LilVillains, lilVillains, LilVillainsStaking;
let admin, minter, royalty, nftOwner;
before(async () => {
[admin, minter, royalty, nftOwner] = await ethers.getSigners();
LilVillains = await getLilVillainsContract(admin);
lilVillains = await LilVillains.deploy(minter.address, royalty.address, baseUrl, 'Collection to test');
await lilVillains.deployed();
await lilVillains.connect(minter).batchMint(nftOwner.address, [1, 2, 3]);
});
let tokenUriResponse;
it('GIVEN: collection already revealed', async () => {
await lilVillains.setBaseURI(baseUrlRevealed);
});
it('WHEN I retrieve token URI of NFT#1', () => {
tokenUriResponse = lilVillains.tokenURI(1);
});
it('THEN I should get the metadata of my NFT', async () => {
expect(await tokenUriResponse).to.be.equal(`${baseUrlRevealed}1`);
});
});
describe.only('Scenario: I already minted NFT#2 and I started staking it', () => {
let LilVillains, lilVillains, LilVillainsStaking;
let admin, minter, royalty, nftOwner;
let tokenUriResponse;
before(async () => {
[admin, minter, royalty, nftOwner] = await ethers.getSigners();
LilVillains = await getLilVillainsContract(admin);
lilVillains = await LilVillains.deploy(minter.address, royalty.address, baseUrl, 'Collection to test');
await lilVillains.deployed();
await lilVillains.connect(minter).batchMint(nftOwner.address, [1, 2, 3]);
LilVillainsStaking = await ethers.getContractFactory('LilVillainsStaking', admin);
lilVillainsStaking = await LilVillainsStaking.deploy();
await lilVillainsStaking.deployed();
await lilVillains.setAttributesControllerSC(lilVillainsStaking.address);
await lilVillains.setTransferControllerSC(lilVillainsStaking.address);
await lilVillains.setBaseURI(baseUrlRevealed);
const ALTER_ATTR_ROLE = await lilVillains.ALTER_ATTR_ROLE();
await lilVillains.grantRole(ALTER_ATTR_ROLE, lilVillainsStaking.address);
await lilVillains.connect(lilVillainsStaking).setBaseAttributes(2, baseAttributes);
});
it('WHEN I retrieve token URI of NFT#2', () => {
tokenUriResponse = lilVillains.tokenURI(2);
});
it('THEN I should get the metadata of my NFT', async () => {
const metadataInJSON = parseTokenUriJson(await tokenUriResponse);
expect(metadataInJSON.attributes[0]['display_type']).to.be.equal(baseAttributes[0].displayType);
expect(metadataInJSON.attributes[0]['trait_type']).to.be.equal(baseAttributes[0].traitType);
expect(metadataInJSON.attributes[0].value).to.be.equal(baseAttributes[0].value);
expect(metadataInJSON.attributes[1]['display_type']).to.be.equal(baseAttributes[1].displayType);
expect(metadataInJSON.attributes[1]['trait_type']).to.be.equal(baseAttributes[1].traitType);
expect(metadataInJSON.attributes[1].value).to.be.equal(baseAttributes[1].value);
});
});
});