diff --git a/package.json b/package.json index 5a2a7c5f..0a58aa5e 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "prepare": "npm run build", "pretty": "prettier --config ./.prettierrc.json --check \"**/*.{ts,json}\"", "pretty:fix": "prettier --config ./.prettierrc.json --write \"**/*.{ts,json}\"", - "lint": "eslint . --ext .ts" + "lint": "eslint . --ext .ts", + "test": "ts-node ./test/nonceUtils.test.ts" } } diff --git a/test/nonceUtils.test.ts b/test/nonceUtils.test.ts new file mode 100644 index 00000000..9e3085e8 --- /dev/null +++ b/test/nonceUtils.test.ts @@ -0,0 +1,30 @@ +import assert from 'assert'; +import { NonceUtils } from '../lib/utils/nonceUtils'; +import { NonceType } from '../lib/interfaces'; + +// Mock Query object with only getAnchorNonce implemented +const mockQuery = { + getAnchorNonce: async (chainId: string): Promise => `anchor-${chainId}` +} as any; + +(async () => { + // Expect error when chainId is missing + let threw = false; + try { + NonceUtils.createNonceFetcher({ nonceType: NonceType.Anchor, nonceParams: {} }, mockQuery); + } catch (err) { + threw = true; + } + assert.ok(threw, 'Expected error when chainId is missing'); + + // Expect returned function to call getAnchorNonce with provided chainId + const fetcher = NonceUtils.createNonceFetcher( + { nonceType: NonceType.Anchor, nonceParams: { chainId: '5' } }, + mockQuery + ); + const value = await fetcher(); + assert.strictEqual(value, 'anchor-5'); + + console.log('All tests passed'); +})(); +