|
| 1 | +import "module-alias/register"; |
| 2 | +import { BigNumber } from "ethers"; |
| 3 | + |
| 4 | +import { Address } from "@utils/types"; |
| 5 | +import { MAX_UINT_256 } from "@utils/constants"; |
| 6 | +import { StringArrayUtilsMock } from "@utils/contracts/index"; |
| 7 | +import DeployHelper from "@utils/deploys"; |
| 8 | +import { |
| 9 | + addSnapshotBeforeRestoreAfterEach, |
| 10 | + getAccounts, |
| 11 | + getWaffleExpect, |
| 12 | +} from "@utils/test/index"; |
| 13 | + |
| 14 | +const expect = getWaffleExpect(); |
| 15 | + |
| 16 | +describe("StringArrayUtils", () => { |
| 17 | + let stringOne: string; |
| 18 | + let stringTwo: string; |
| 19 | + let stringThree: string; |
| 20 | + let unincludedString: string; |
| 21 | + let deployer: DeployHelper; |
| 22 | + |
| 23 | + let stringArrayUtils: StringArrayUtilsMock; |
| 24 | + |
| 25 | + let baseArray: Address[]; |
| 26 | + |
| 27 | + before(async () => { |
| 28 | + |
| 29 | + stringOne = "eth"; |
| 30 | + stringTwo = "to"; |
| 31 | + stringThree = "$10k"; |
| 32 | + |
| 33 | + unincludedString = "$0"; |
| 34 | + |
| 35 | + const [ owner ] = await getAccounts(); |
| 36 | + |
| 37 | + deployer = new DeployHelper(owner.wallet); |
| 38 | + stringArrayUtils = await deployer.mocks.deployStringArrayUtilsMock(); |
| 39 | + |
| 40 | + baseArray = [ stringOne, stringTwo, stringThree ]; |
| 41 | + }); |
| 42 | + |
| 43 | + addSnapshotBeforeRestoreAfterEach(); |
| 44 | + |
| 45 | + describe("#indexOf", async () => { |
| 46 | + let subjectArray: string[]; |
| 47 | + let subjectString: string; |
| 48 | + |
| 49 | + beforeEach(async () => { |
| 50 | + subjectArray = baseArray; |
| 51 | + subjectString = stringTwo; |
| 52 | + }); |
| 53 | + |
| 54 | + async function subject(): Promise<any> { |
| 55 | + return stringArrayUtils.testIndexOf(subjectArray, subjectString); |
| 56 | + } |
| 57 | + |
| 58 | + it("should return the correct index and true", async () => { |
| 59 | + const [index, isIn] = await subject(); |
| 60 | + |
| 61 | + expect(index).to.eq(BigNumber.from(1)); |
| 62 | + expect(isIn).to.be.true; |
| 63 | + }); |
| 64 | + |
| 65 | + describe("when passed address is not in array", async () => { |
| 66 | + beforeEach(async () => { |
| 67 | + subjectString = unincludedString; |
| 68 | + }); |
| 69 | + |
| 70 | + it("should return false and max number index", async () => { |
| 71 | + const [index, isIn] = await subject(); |
| 72 | + |
| 73 | + expect(index).to.eq(MAX_UINT_256); |
| 74 | + expect(isIn).to.be.false; |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("#removeStorage", async () => { |
| 80 | + let subjectString: string; |
| 81 | + |
| 82 | + beforeEach(async () => { |
| 83 | + await stringArrayUtils.setStorageArray(baseArray); |
| 84 | + subjectString = stringTwo; |
| 85 | + }); |
| 86 | + |
| 87 | + async function subject(): Promise<any> { |
| 88 | + return stringArrayUtils.testRemoveStorage(subjectString); |
| 89 | + } |
| 90 | + |
| 91 | + it("should make the correct updates to the storage array", async () => { |
| 92 | + await subject(); |
| 93 | + |
| 94 | + const actualArray = await stringArrayUtils.getStorageArray(); |
| 95 | + expect(JSON.stringify(actualArray)).to.eq(JSON.stringify([ stringOne, stringThree ])); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("when item being removed is last in array", async () => { |
| 99 | + beforeEach(async () => { |
| 100 | + subjectString = stringThree; |
| 101 | + }); |
| 102 | + |
| 103 | + it("should just pop off last item", async () => { |
| 104 | + await subject(); |
| 105 | + |
| 106 | + const actualArray = await stringArrayUtils.getStorageArray(); |
| 107 | + expect(JSON.stringify(actualArray)).to.eq(JSON.stringify([ stringOne, stringTwo ])); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("when passed address is not in array", async () => { |
| 112 | + beforeEach(async () => { |
| 113 | + subjectString = unincludedString; |
| 114 | + }); |
| 115 | + |
| 116 | + it("should revert", async () => { |
| 117 | + await expect(subject()).to.be.revertedWith("String not in array."); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments