Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit c506ed9

Browse files
committed
No need to observe the token again
1 parent 08166bb commit c506ed9

File tree

2 files changed

+38
-62
lines changed

2 files changed

+38
-62
lines changed

lib/containers/github-tab-header-container.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import {QueryRenderer, graphql} from 'react-relay';
44

5-
import {EndpointPropType} from '../prop-types';
5+
import {EndpointPropType, TokenPropType} from '../prop-types';
66
import RelayNetworkLayerManager from '../relay-network-layer-manager';
77
import {UNAUTHENTICATED, INSUFFICIENT} from '../shared/keytar-strategy';
8-
import ObserveModel from '../views/observe-model';
98
import Author, {nullAuthor} from '../models/author';
109
import GithubTabHeaderController from '../controllers/github-tab-header-controller';
1110

1211
export default class GithubTabHeaderContainer extends React.Component {
1312
static propTypes = {
1413
// Connection
15-
loginModel: PropTypes.object.isRequired,
1614
endpoint: EndpointPropType.isRequired,
15+
token: TokenPropType,
1716

1817
// Workspace
1918
currentWorkDir: PropTypes.string,
@@ -27,24 +26,16 @@ export default class GithubTabHeaderContainer extends React.Component {
2726
}
2827

2928
render() {
30-
return (
31-
<ObserveModel model={this.props.loginModel} fetchData={this.fetchToken}>
32-
{this.renderWithToken}
33-
</ObserveModel>
34-
);
35-
}
36-
37-
renderWithToken = token => {
3829
if (
39-
token == null
40-
|| token instanceof Error
41-
|| token === UNAUTHENTICATED
42-
|| token === INSUFFICIENT
30+
this.props.token == null
31+
|| this.props.token instanceof Error
32+
|| this.props.token === UNAUTHENTICATED
33+
|| this.props.token === INSUFFICIENT
4334
) {
4435
return this.renderNoResult();
4536
}
4637

47-
const environment = RelayNetworkLayerManager.getEnvironmentForHost(this.props.endpoint, token);
38+
const environment = RelayNetworkLayerManager.getEnvironmentForHost(this.props.endpoint, this.props.token);
4839
const query = graphql`
4940
query githubTabHeaderContainerQuery {
5041
viewer {
@@ -61,12 +52,12 @@ export default class GithubTabHeaderContainer extends React.Component {
6152
environment={environment}
6253
variables={{}}
6354
query={query}
64-
render={result => this.renderWithResult(result)}
55+
render={this.renderWithResult}
6556
/>
6657
);
6758
}
6859

69-
renderWithResult({error, props}) {
60+
renderWithResult = ({error, props}) => {
7061
if (error || props === null) {
7162
return this.renderNoResult();
7263
}
@@ -108,8 +99,4 @@ export default class GithubTabHeaderContainer extends React.Component {
10899
/>
109100
);
110101
}
111-
112-
fetchToken = loginModel => {
113-
return loginModel.getToken(this.props.endpoint.getLoginAccount());
114-
}
115102
}

test/containers/github-tab-header-container.test.js

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@ import {QueryRenderer} from 'react-relay';
44

55
import GithubTabHeaderContainer from '../../lib/containers/github-tab-header-container';
66
import {queryBuilder} from '../builder/graphql/query';
7-
import GithubLoginModel from '../../lib/models/github-login-model';
8-
import {getEndpoint} from '../../lib/models/endpoint';
9-
import {InMemoryStrategy, INSUFFICIENT, UNAUTHENTICATED} from '../../lib/shared/keytar-strategy';
7+
import {DOTCOM} from '../../lib/models/endpoint';
8+
import {UNAUTHENTICATED, INSUFFICIENT} from '../../lib/shared/keytar-strategy';
109

1110
import tabHeaderQuery from '../../lib/containers/__generated__/githubTabHeaderContainerQuery.graphql';
1211

1312
describe('GithubTabHeaderContainer', function() {
14-
let atomEnv, model;
13+
let atomEnv;
1514

1615
beforeEach(function() {
1716
atomEnv = global.buildAtomEnvironment();
18-
model = new GithubLoginModel(InMemoryStrategy);
1917
});
2018

2119
afterEach(function() {
@@ -25,65 +23,56 @@ describe('GithubTabHeaderContainer', function() {
2523
function buildApp(overrideProps = {}) {
2624
return (
2725
<GithubTabHeaderContainer
28-
loginModel={model}
29-
endpoint={getEndpoint('github.com')}
26+
endpoint={DOTCOM}
27+
token="1234"
28+
3029
currentWorkDir={null}
3130
contextLocked={false}
3231
changeWorkingDirectory={() => {}}
3332
setContextLock={() => {}}
3433
getCurrentWorkDirs={() => new Set()}
34+
3535
onDidChangeWorkDirs={() => {}}
3636
{...overrideProps}
3737
/>
3838
);
3939
}
4040

41-
it('renders a null user while the GraphQL query is being performed', async function() {
42-
model.setToken('https://api.github.com', '1234');
43-
44-
sinon.spy(model, 'getToken');
45-
sinon.stub(model, 'getScopes').resolves(GithubLoginModel.REQUIRED_SCOPES);
46-
47-
const wrapper = shallow(buildApp());
48-
49-
assert.strictEqual(await wrapper.find('ObserveModel').prop('fetchData')(model), '1234');
50-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')('1234');
51-
52-
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
53-
error: null,
54-
props: null,
55-
retry: () => {},
56-
});
57-
58-
assert.isFalse(resultWrapper.find('GithubTabHeaderController').prop('user').isPresent());
41+
it('renders a null user if the token is still loading', function() {
42+
const wrapper = shallow(buildApp({token: null}));
43+
assert.isFalse(wrapper.find('GithubTabHeaderController').prop('user').isPresent());
5944
});
6045

6146
it('renders a null user if no token is found', function() {
62-
const wrapper = shallow(buildApp());
63-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(UNAUTHENTICATED);
64-
assert.isFalse(tokenWrapper.find('GithubTabHeaderController').prop('user').isPresent());
47+
const wrapper = shallow(buildApp({token: UNAUTHENTICATED}));
48+
assert.isFalse(wrapper.find('GithubTabHeaderController').prop('user').isPresent());
6549
});
6650

6751
it('renders a null user if the token has insufficient OAuth scopes', function() {
68-
const wrapper = shallow(buildApp());
69-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(INSUFFICIENT);
70-
71-
assert.isFalse(tokenWrapper.find('GithubTabHeaderController').prop('user').isPresent());
52+
const wrapper = shallow(buildApp({token: INSUFFICIENT}));
53+
assert.isFalse(wrapper.find('GithubTabHeaderController').prop('user').isPresent());
7254
});
7355

74-
it('renders a null user if the user is offline', function() {
75-
sinon.spy(model, 'didUpdate');
56+
it('renders a null user if there was an error acquiring the token', function() {
57+
const e = new Error('oops');
58+
e.rawStack = e.stack;
59+
const wrapper = shallow(buildApp({token: e}));
60+
assert.isFalse(wrapper.find('GithubTabHeaderController').prop('user').isPresent());
61+
});
7662

63+
it('renders a null user while the GraphQL query is being performed', function() {
7764
const wrapper = shallow(buildApp());
78-
const e = new Error('oh no');
79-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(e);
80-
assert.isFalse(tokenWrapper.find('GithubTabHeaderController').prop('user').isPresent());
65+
const resultWrapper = wrapper.find(QueryRenderer).renderProp('render')({
66+
error: null,
67+
props: null,
68+
retry: () => {},
69+
});
70+
71+
assert.isFalse(resultWrapper.find('GithubTabHeaderController').prop('user').isPresent());
8172
});
8273

8374
it('renders the controller once results have arrived', function() {
8475
const wrapper = shallow(buildApp());
85-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')('1234');
86-
8776
const props = queryBuilder(tabHeaderQuery)
8877
.viewer(v => {
8978
v.name('user');
@@ -92,7 +81,7 @@ describe('GithubTabHeaderContainer', function() {
9281
v.login('us3rh4nd13');
9382
})
9483
.build();
95-
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({error: null, props, retry: () => {}});
84+
const resultWrapper = wrapper.find(QueryRenderer).renderProp('render')({error: null, props, retry: () => {}});
9685

9786
const controller = resultWrapper.find('GithubTabHeaderController');
9887
assert.isTrue(controller.prop('user').isPresent());

0 commit comments

Comments
 (0)