|
1 | 1 | import React from 'react';
|
2 |
| -import {mount} from 'enzyme'; |
| 2 | +import {mount, shallow} from 'enzyme'; |
3 | 3 |
|
4 |
| -import {cloneRepository} from '../helpers'; |
| 4 | +import {buildRepository, cloneRepository} from '../helpers'; |
5 | 5 | import GitHubTabContainer from '../../lib/containers/github-tab-container';
|
| 6 | +import GitHubTabController from '../../lib/controllers/github-tab-controller'; |
6 | 7 | import Repository from '../../lib/models/repository';
|
7 | 8 | import {InMemoryStrategy} from '../../lib/shared/keytar-strategy';
|
8 | 9 | import GithubLoginModel from '../../lib/models/github-login-model';
|
9 | 10 | import RefHolder from '../../lib/models/ref-holder';
|
10 | 11 |
|
11 | 12 | describe('GitHubTabContainer', function() {
|
12 |
| - let atomEnv, repository; |
| 13 | + let atomEnv, repository, defaultRepositoryData; |
13 | 14 |
|
14 |
| - beforeEach(function() { |
| 15 | + beforeEach(async function() { |
15 | 16 | atomEnv = global.buildAtomEnvironment();
|
16 |
| - repository = Repository.absent(); |
| 17 | + repository = await buildRepository(await cloneRepository()); |
| 18 | + |
| 19 | + defaultRepositoryData = { |
| 20 | + workingDirectory: repository.getWorkingDirectoryPath(), |
| 21 | + allRemotes: await repository.getRemotes(), |
| 22 | + branches: await repository.getBranches(), |
| 23 | + selectedRemoteName: 'origin', |
| 24 | + aheadCount: 0, |
| 25 | + pushInProgress: false, |
| 26 | + }; |
17 | 27 | });
|
18 | 28 |
|
19 | 29 | afterEach(function() {
|
@@ -41,26 +51,66 @@ describe('GitHubTabContainer', function() {
|
41 | 51 | );
|
42 | 52 | }
|
43 | 53 |
|
44 |
| - describe('operation state observer', function() { |
45 |
| - it('creates an observer on the current repository', function() { |
46 |
| - const wrapper = mount(buildApp()); |
| 54 | + describe('refresher', function() { |
| 55 | + let wrapper, retry; |
| 56 | + |
| 57 | + function stubRepository(repo) { |
| 58 | + sinon.stub(repo.getOperationStates(), 'isFetchInProgress').returns(false); |
| 59 | + sinon.stub(repo.getOperationStates(), 'isPushInProgress').returns(false); |
| 60 | + sinon.stub(repo.getOperationStates(), 'isPullInProgress').returns(false); |
| 61 | + } |
| 62 | + |
| 63 | + function simulateOperation(repo, name, middle = () => {}) { |
| 64 | + const accessor = `is${name[0].toUpperCase()}${name.slice(1)}InProgress`; |
| 65 | + const methodStub = repo.getOperationStates()[accessor]; |
| 66 | + methodStub.returns(true); |
| 67 | + repo.state.didUpdate(); |
| 68 | + middle(); |
| 69 | + methodStub.returns(false); |
| 70 | + repo.state.didUpdate(); |
| 71 | + } |
| 72 | + |
| 73 | + beforeEach(function() { |
| 74 | + wrapper = shallow(buildApp()); |
| 75 | + const childWrapper = wrapper.find('ObserveModel').renderProp('children')(defaultRepositoryData); |
| 76 | + |
| 77 | + retry = sinon.spy(); |
| 78 | + const refresher = childWrapper.find(GitHubTabController).prop('refresher'); |
| 79 | + refresher.setRetryCallback(Symbol('key'), retry); |
| 80 | + |
| 81 | + stubRepository(repository); |
| 82 | + }); |
| 83 | + |
| 84 | + it('triggers a refresh when the current repository completes a fetch, push, or pull', function() { |
| 85 | + assert.isFalse(retry.called); |
| 86 | + |
| 87 | + simulateOperation(repository, 'fetch', () => assert.isFalse(retry.called)); |
| 88 | + assert.strictEqual(retry.callCount, 1); |
47 | 89 |
|
48 |
| - const observer = wrapper.state('remoteOperationObserver'); |
49 |
| - assert.strictEqual(observer.repository, repository); |
| 90 | + simulateOperation(repository, 'push', () => assert.strictEqual(retry.callCount, 1)); |
| 91 | + assert.strictEqual(retry.callCount, 2); |
50 | 92 |
|
51 |
| - wrapper.setProps({}); |
52 |
| - assert.strictEqual(wrapper.state('remoteOperationObserver'), observer); |
| 93 | + simulateOperation(repository, 'pull', () => assert.strictEqual(retry.callCount, 2)); |
| 94 | + assert.strictEqual(retry.callCount, 3); |
53 | 95 | });
|
54 | 96 |
|
55 |
| - it('creates a new observer when the repository changes', function() { |
56 |
| - const repository0 = Repository.absent(); |
57 |
| - const wrapper = mount(buildApp({repository: repository0})); |
| 97 | + it('un-observes an old repository and observes a new one', async function() { |
| 98 | + const other = await buildRepository(await cloneRepository()); |
| 99 | + stubRepository(other); |
| 100 | + wrapper.setProps({repository: other}); |
| 101 | + |
| 102 | + simulateOperation(repository, 'fetch'); |
| 103 | + assert.isFalse(retry.called); |
| 104 | + |
| 105 | + simulateOperation(other, 'fetch'); |
| 106 | + assert.isTrue(retry.called); |
| 107 | + }); |
58 | 108 |
|
59 |
| - const observer0 = wrapper.state('remoteOperationObserver'); |
| 109 | + it('un-observes the repository when unmounting', function() { |
| 110 | + wrapper.unmount(); |
60 | 111 |
|
61 |
| - const repository1 = Repository.absent(); |
62 |
| - wrapper.setProps({repository: repository1}); |
63 |
| - assert.notStrictEqual(wrapper.state('remoteOperationObserver'), observer0); |
| 112 | + simulateOperation(repository, 'fetch'); |
| 113 | + assert.isFalse(retry.called); |
64 | 114 | });
|
65 | 115 | });
|
66 | 116 |
|
|
0 commit comments