test-contract.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* eslint no-undef: "off" */
  2. /* eslint arrow-body-style: ["error", "always"] */
  3. const ShowTrader = artifacts.require('ShowTrader')
  4. contract('ShowTrader Test', (accounts) => {
  5. let trader
  6. const danceData = '0x111000'
  7. const musicData = '0x000111'
  8. it('should create 1 show', () => {
  9. const player = accounts[0]
  10. const showName = '1st show'
  11. return ShowTrader.deployed()
  12. .then((instance) => {
  13. trader = instance
  14. return trader.createFirstShow(showName, danceData, musicData, { from: player })
  15. })
  16. .then(() => {
  17. return trader.getShowsByOwner.call(player)
  18. })
  19. .then((shows) => {
  20. const id = shows[0].toNumber()
  21. return trader.shows(id)
  22. })
  23. .then((show) => {
  24. assert.equal(show[0], showName)
  25. assert.equal(show[1], danceData)
  26. assert.equal(show[2], musicData)
  27. })
  28. })
  29. // The second test begin...
  30. it('shoud exchange 2 shows', () => {
  31. const name1 = 'show1'
  32. const name2 = 'show2'
  33. const player1 = accounts[1] // The account[0] is used for the 1st test
  34. // Note: 'VM Exception while processing transaction: revert',
  35. // usually means that the require() statement is not met
  36. const player2 = accounts[2]
  37. let id1
  38. let id2
  39. return ShowTrader.deployed()
  40. .then((instance) => {
  41. trader = instance
  42. return trader.createFirstShow(name1, danceData, musicData, { from: player1 })
  43. })
  44. .then(() => {
  45. return trader.createFirstShow(name2, danceData, musicData, { from: player2 })
  46. })
  47. .then(() => {
  48. return trader.getShowsByOwner.call(player1)
  49. })
  50. .then((shows1) => {
  51. id1 = shows1[0].toNumber()
  52. return trader.getShowsByOwner.call(player2)
  53. })
  54. .then((shows2) => {
  55. id2 = shows2[0].toNumber()
  56. return trader.wantToExchange(id1, id2, { from: player1 })
  57. })
  58. .then(() => {
  59. return trader.wantToExchange(id2, id1, { from: player2 })
  60. })
  61. .then(() => {
  62. return trader.showToOwner(id1)
  63. })
  64. .then((owner1) => {
  65. assert.equal(owner1, player2, 'owner of show1 should be player2!')
  66. return trader.showToOwner(id2)
  67. })
  68. .then((owner2) => {
  69. assert.equal(owner2, player1, 'owner of show2 should be player1!')
  70. })
  71. })
  72. })