-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from near/contract-fn-binding
improve usability of contract functions
- Loading branch information
Showing
4 changed files
with
98 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { Contract } = require('../lib/contract'); | ||
const { PositionalArgsError } = require('../lib/utils/errors'); | ||
|
||
const account = { | ||
viewFunction() { | ||
return this; | ||
}, | ||
functionCall() { | ||
return this; | ||
} | ||
}; | ||
|
||
const contract = new Contract(account, null, { | ||
viewMethods: ['viewMethod'], | ||
changeMethods: ['changeMethod'], | ||
}); | ||
|
||
['viewMethod', 'changeMethod'].forEach(method => { | ||
describe(method, () => { | ||
test('returns what you expect for .name', () => { | ||
expect(contract[method].name).toBe(method); | ||
}); | ||
|
||
test('maintains correct reference to `this` when passed around an application', async () => { | ||
function callFuncInNewContext(fn) { | ||
return fn(); | ||
} | ||
expect(await callFuncInNewContext(contract[method])); | ||
}); | ||
|
||
test('throws PositionalArgsError if first argument is not an object', () => { | ||
return Promise.all([ | ||
1, | ||
'lol', | ||
[], | ||
new Date(), | ||
null, | ||
new Set(), | ||
].map(async badArgs => { | ||
try { | ||
await contract[method](badArgs); | ||
throw new Error(`Calling \`contract.${method}(${badArgs})\` worked. It shouldn't have worked.`); | ||
} catch (e) { | ||
if (!(e instanceof PositionalArgsError)) throw e; | ||
} | ||
})); | ||
}); | ||
|
||
test('throws PositionalArgsError if given too many arguments', () => { | ||
return expect(contract[method]({}, 1, 0, 'oops')).rejects.toBeInstanceOf(PositionalArgsError); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('changeMethod', () => { | ||
test('throws error message for invalid gas argument', () => { | ||
return expect(contract.changeMethod({ a: 1}, 'whatever')).rejects.toThrow(/Expected number, decimal string or BN for 'gas' argument, but got.+/); | ||
}); | ||
|
||
test('gives error message for invalid amount argument', () => { | ||
return expect(contract.changeMethod({ a: 1}, 1000, 'whatever')).rejects.toThrow(/Expected number, decimal string or BN for 'amount' argument, but got.+/); | ||
}); | ||
|
||
}); |