aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/pages/first-time-flow/seed-phrase/tests/confirm-seed-phrase-component.test.js
blob: 3d5f7f0662bbb8004d24e71b7a0df5211db0ca15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React from 'react'
import assert from 'assert'
import { shallow } from 'enzyme'
import sinon from 'sinon'
import ConfirmSeedPhrase from '../confirm-seed-phrase/confirm-seed-phrase.component'

function shallowRender (props = {}, context = {}) {
  return shallow(
    <ConfirmSeedPhrase {...props} />,
    {
      context: {
        t: str => str + '_t',
        ...context,
      },
    }
  )
}

describe('ConfirmSeedPhrase Component', () => {
  it('should render correctly', () => {
    const root = shallowRender({
      seedPhrase: '鼠 牛 虎 兔 龍 蛇 馬 羊 猴 雞 狗 豬',
    })

    assert.equal(
      root.find('.confirm-seed-phrase__seed-word--shuffled').length,
      12,
      'should render 12 seed phrases'
    )
  })

  it('should add/remove selected on click', () => {
    const metricsEventSpy = sinon.spy()
    const pushSpy = sinon.spy()
    const root = shallowRender(
      {
        seedPhrase: '鼠 牛 虎 兔 龍 蛇 馬 羊 猴 雞 狗 豬',
        history: { push: pushSpy },
      },
      {
        metricsEvent: metricsEventSpy,
      }
    )

    const seeds = root.find('.confirm-seed-phrase__seed-word--shuffled')

    // Click on 3 seeds to add to selected
    seeds.at(0).simulate('click')
    seeds.at(1).simulate('click')
    seeds.at(2).simulate('click')

    assert.deepEqual(
      root.state().selectedSeedIndices,
      [0, 1, 2],
      'should add seed phrase to selected on click',
    )

    // Click on a selected seed to remove
    root.state()
    root.update()
    root.state()
    root.find('.confirm-seed-phrase__seed-word--shuffled').at(1).simulate('click')
    assert.deepEqual(
      root.state().selectedSeedIndices,
      [0, 2],
      'should remove seed phrase from selected when click again',
    )
  })

  it('should render correctly on hover', () => {
    const metricsEventSpy = sinon.spy()
    const pushSpy = sinon.spy()
    const root = shallowRender(
      {
        seedPhrase: '鼠 牛 虎 兔 龍 蛇 馬 羊 猴 雞 狗 豬',
        history: { push: pushSpy },
      },
      {
        metricsEvent: metricsEventSpy,
      }
    )

    const seeds = root.find('.confirm-seed-phrase__seed-word--shuffled')

    // Click on 3 seeds to add to selected
    seeds.at(0).simulate('click')
    seeds.at(1).simulate('click')
    seeds.at(2).simulate('click')

    // Dragging Seed # 2 to 0 placeth
    root.instance().setDraggingSeedIndex(2)
    root.instance().setHoveringIndex(0)

    root.update()

    const pendingSeeds = root.find('.confirm-seed-phrase__selected-seed-words__pending-seed')

    assert.equal(pendingSeeds.at(0).props().seedIndex, 2)
    assert.equal(pendingSeeds.at(1).props().seedIndex, 0)
    assert.equal(pendingSeeds.at(2).props().seedIndex, 1)
  })

  it('should insert seed in place on drop', () => {
    const metricsEventSpy = sinon.spy()
    const pushSpy = sinon.spy()
    const root = shallowRender(
      {
        seedPhrase: '鼠 牛 虎 兔 龍 蛇 馬 羊 猴 雞 狗 豬',
        history: { push: pushSpy },
      },
      {
        metricsEvent: metricsEventSpy,
      }
    )

    const seeds = root.find('.confirm-seed-phrase__seed-word--shuffled')

    // Click on 3 seeds to add to selected
    seeds.at(0).simulate('click')
    seeds.at(1).simulate('click')
    seeds.at(2).simulate('click')

    // Drop Seed # 2 to 0 placeth
    root.instance().setDraggingSeedIndex(2)
    root.instance().setHoveringIndex(0)
    root.instance().onDrop(0)

    root.update()

    assert.deepEqual(root.state().selectedSeedIndices, [2, 0, 1])
    assert.deepEqual(root.state().pendingSeedIndices, [2, 0, 1])
  })

  it('should submit correctly', async () => {
    const originalSeed = ['鼠', '牛', '虎', '兔', '龍', '蛇', '馬', '羊', '猴', '雞', '狗', '豬']
    const metricsEventSpy = sinon.spy()
    const pushSpy = sinon.spy()
    const root = shallowRender(
      {
        seedPhrase: '鼠 牛 虎 兔 龍 蛇 馬 羊 猴 雞 狗 豬',
        history: { push: pushSpy },
        setSeedPhraseBackedUp: () => Promise.resolve(),
      },
      {
        metricsEvent: metricsEventSpy,
      }
    )

    const shuffled = root.state().shuffledSeedWords
    const seeds = root.find('.confirm-seed-phrase__seed-word--shuffled')


    originalSeed.forEach(seed => {
      const seedIndex = shuffled.findIndex(s => s === seed)
      seeds.at(seedIndex).simulate('click')
    })

    root.update()

    root.find('.first-time-flow__button').simulate('click')

    await (new Promise(resolve => setTimeout(resolve, 100)))

    assert.deepEqual(metricsEventSpy.args[0][0], {
      eventOpts: {
        category: 'Onboarding',
        action: 'Seed Phrase Setup',
        name: 'Verify Complete',
      },
    })
    assert.equal(pushSpy.args[0][0], '/initialize/end-of-flow')
  })
})