Skip to content
This repository has been archived by the owner on Apr 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #525 from common-group/fix/user-undefined-global
Browse files Browse the repository at this point in the history
🐞 carregar usuário antes do carregamento da página para evitar user u…
  • Loading branch information
thiagocatarse authored Oct 15, 2020
2 parents 95913b5 + 1051475 commit fa47bf0
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 178 deletions.
71 changes: 71 additions & 0 deletions legacy/spec/components/wrap.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import m from 'mithril'
import mq from 'mithril-query'
import { Wrap } from '../../src/wrap'
import userVM from '../../src/vms/user-vm'
import '../lib/mocks/user-details.mock'

describe('Wrap', () => {
describe('view', () => {

beforeAll(() => {
window.optimizeObserver = {
addListener() { }
}
})

it('should display component after user is loaded', async () => {
// 1. arrange
const userMocked = UserDetailMockery()[0]
const user_id = userMocked.id
jasmine.Ajax
.stubRequest(`${apiPrefix}/user_details`)
.andReturn({
responseText: `[{"id":${user_id},"name":"User Name"}]`,
})

const component = mq(Wrap(TestComponent, { user_id }))

// this is a hack to wait inner promises to resolve
await sleep(0)
component.redraw()
// this is a hack to wait redraw execute after promises be resolved
await sleep(0)

// 2. act

// 3. assert
component.should.contain('INNER COMPONENT CONTENT')
})

it('should not display component before user is loaded', async () => {
// 1. arrange
const userMocked = UserDetailMockery()[0]
const user_id = userMocked.id
jasmine.Ajax
.stubRequest(`${apiPrefix}/user_details`)
.andReturn({
responseText: `[{"id":${user_id},"name":"User Name"}]`,
})

const component = mq(Wrap(TestComponent, { user_id }))

// 2. act

// 3. assert
component.should.not.contain('INNER COMPONENT CONTENT')
})

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

})
})

class TestComponent implements m.Component<{}> {
view() {
return (
<div id='inner-component'>INNER COMPONENT CONTENT</div>
)
}
}
126 changes: 63 additions & 63 deletions legacy/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import h from './h';
import _ from 'underscore';
import c from './c';
import Chart from 'chart.js';
import { wrap } from './wrap';
import { Wrap } from './wrap';

m.originalTrust = m.trust;
m.trust = (text) => h.trust(text);
Expand Down Expand Up @@ -93,81 +93,81 @@ m.trust = (text) => h.trust(text);
*/

m.route(rootEl, '/', {
'/': wrap(isUserProfile ? c.root.UsersShow : c.root.ProjectsHome, { menuTransparency: true, footerBig: true, absoluteHome: isUserProfile }),
'/explore': wrap(c.root.ProjectsExplore, { menuTransparency: true, footerBig: true }),
'/start': wrap(c.root.Start, { menuTransparency: true, footerBig: true }),
'/start-sub': wrap(c.root.SubProjectNew, { menuTransparency: false }),
'/projects/:project_id/contributions/new': wrap(c.root.ProjectsContribution),
'/projects/:project_id/contributions/fallback_create': wrap(c.root.ProjectsContribution),
'/projects/:project_id/contributions/:contribution_id/edit': wrap(c.root.ProjectsPayment, { menuShort: true }),
'/projects/:project_id/subscriptions/start': wrap(c.root.ProjectsSubscriptionContribution, { menuShort: true, footerBig: false }),
'/projects/:project_id/subscriptions/checkout': wrap(c.root.ProjectsSubscriptionCheckout, { menuShort: true, footerBig: false }),
'/projects/:project_id/subscriptions/thank_you': wrap(c.root.ProjectsSubscriptionThankYou, { menuShort: true, footerBig: false }),
[urlWithLocale('/projects/:project_id/contributions/new')]: wrap(c.root.ProjectsContribution),
[urlWithLocale('/projects/:project_id/contributions/:contribution_id/edit')]: wrap(c.root.ProjectsPayment, { menuShort: true }),
[urlWithLocale('/projects/:project_id/subscriptions/start')]: wrap(c.root.ProjectsSubscriptionContribution, { menuShort: true, footerBig: false }),
[urlWithLocale('/projects/:project_id/subscriptions/checkout')]: wrap(c.root.ProjectsSubscriptionCheckout, { menuShort: true, footerBig: false }),
[urlWithLocale('/projects/subscriptions/thank_you')]: wrap(c.root.ProjectsSubscriptionThankYou, { menuShort: true, footerBig: false }),
'/en': wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
'/pt': wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/flexible_projects')]: wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/projects')]: wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
'/projects': wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/explore')]: wrap(c.root.ProjectsExplore, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/start')]: wrap(c.root.Start, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/projects/:project_id/contributions/:contribution_id')]: wrap(c.root.ThankYou, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/contributions/:contribution_id': wrap(c.root.ThankYou, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/insights': wrap(c.root.Insights, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/insights')]: wrap(c.root.Insights, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/contributions_report': wrap(c.root.ProjectsContributionReport, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/contributions_report')]: wrap(c.root.ProjectsContributionReport, {
'/': Wrap(isUserProfile ? c.root.UsersShow : c.root.ProjectsHome, { menuTransparency: true, footerBig: true, absoluteHome: isUserProfile }),
'/explore': Wrap(c.root.ProjectsExplore, { menuTransparency: true, footerBig: true }),
'/start': Wrap(c.root.Start, { menuTransparency: true, footerBig: true }),
'/start-sub': Wrap(c.root.SubProjectNew, { menuTransparency: false }),
'/projects/:project_id/contributions/new': Wrap(c.root.ProjectsContribution),
'/projects/:project_id/contributions/fallback_create': Wrap(c.root.ProjectsContribution),
'/projects/:project_id/contributions/:contribution_id/edit': Wrap(c.root.ProjectsPayment, { menuShort: true }),
'/projects/:project_id/subscriptions/start': Wrap(c.root.ProjectsSubscriptionContribution, { menuShort: true, footerBig: false }),
'/projects/:project_id/subscriptions/checkout': Wrap(c.root.ProjectsSubscriptionCheckout, { menuShort: true, footerBig: false }),
'/projects/:project_id/subscriptions/thank_you': Wrap(c.root.ProjectsSubscriptionThankYou, { menuShort: true, footerBig: false }),
[urlWithLocale('/projects/:project_id/contributions/new')]: Wrap(c.root.ProjectsContribution),
[urlWithLocale('/projects/:project_id/contributions/:contribution_id/edit')]: Wrap(c.root.ProjectsPayment, { menuShort: true }),
[urlWithLocale('/projects/:project_id/subscriptions/start')]: Wrap(c.root.ProjectsSubscriptionContribution, { menuShort: true, footerBig: false }),
[urlWithLocale('/projects/:project_id/subscriptions/checkout')]: Wrap(c.root.ProjectsSubscriptionCheckout, { menuShort: true, footerBig: false }),
[urlWithLocale('/projects/subscriptions/thank_you')]: Wrap(c.root.ProjectsSubscriptionThankYou, { menuShort: true, footerBig: false }),
'/en': Wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
'/pt': Wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/flexible_projects')]: Wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/projects')]: Wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
'/projects': Wrap(c.root.ProjectsHome, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/explore')]: Wrap(c.root.ProjectsExplore, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/start')]: Wrap(c.root.Start, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/projects/:project_id/contributions/:contribution_id')]: Wrap(c.root.ThankYou, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/contributions/:contribution_id': Wrap(c.root.ThankYou, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/insights': Wrap(c.root.Insights, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/insights')]: Wrap(c.root.Insights, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/contributions_report': Wrap(c.root.ProjectsContributionReport, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/contributions_report')]: Wrap(c.root.ProjectsContributionReport, {
menuTransparency: false,
footerBig: false,
}),
'/projects/:project_id/subscriptions_report': wrap(c.root.ProjectsSubscriptionReport, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/subscriptions_report')]: wrap(c.root.ProjectsSubscriptionReport, {
'/projects/:project_id/subscriptions_report': Wrap(c.root.ProjectsSubscriptionReport, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/subscriptions_report')]: Wrap(c.root.ProjectsSubscriptionReport, {
menuTransparency: false,
footerBig: false,
}),
'/projects/:project_id/subscriptions_report_download': wrap(c.root.ProjectsSubscriptionReportDownload, {
'/projects/:project_id/subscriptions_report_download': Wrap(c.root.ProjectsSubscriptionReportDownload, {
menuTransparency: false,
footerBig: false,
}),
[urlWithLocale('/projects/:project_id/subscriptions_report_download')]: wrap(c.root.ProjectsSubscriptionReportDownload, {
[urlWithLocale('/projects/:project_id/subscriptions_report_download')]: Wrap(c.root.ProjectsSubscriptionReportDownload, {
menuTransparency: false,
footerBig: false,
}),
'/projects/:project_id/surveys': wrap(c.root.Surveys, { menuTransparency: false, footerBig: false, menuShort: true }),
'/projects/:project_id/fiscal': wrap(c.root.ProjectsFiscal, { menuTransparency: false, footerBig: false, menuShort: true }),
'/projects/:project_id/posts': wrap(c.root.Posts, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/posts/:post_id': wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: true }),
[urlWithLocale('/projects/:project_id/posts')]: wrap(c.root.Posts, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/posts/:post_id')]: wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: true }),
'/projects/:project_id': wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: false }),
'/users/:user_id': wrap(c.root.UsersShow, { menuTransparency: true, footerBig: false }),
[urlWithLocale('/users/:user_id')]: wrap(c.root.UsersShow, { menuTransparency: true, footerBig: false }),
'/contributions/:contribution_id/surveys/:survey_id': wrap(c.root.SurveysShow, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/contributions/:contribution_id/surveys/:survey_id')]: wrap(c.root.SurveysShow, { menuTransparency: false, footerBig: false }),
'/users/:user_id/edit': wrap(c.root.UsersEdit, { menuTransparency: true, footerBig: false }),
[urlWithLocale('/users/:user_id/edit')]: wrap(c.root.UsersEdit, { menuTransparency: true, footerBig: false }),
'/projects/:project_id/edit': wrap(c.root.ProjectEdit, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/projects/:project_id/edit')]: wrap(c.root.ProjectEdit, { menuTransparency: false, hideFooter: true, menuShort: true }),
'/projects/:project_id/rewards/:reward_id/surveys/new': wrap(c.root.SurveyCreate, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/follow-fb-friends')]: wrap(c.root.FollowFoundFriends, { menuTransparency: false, footerBig: false }),
'/follow-fb-friends': wrap(c.root.FollowFoundFriends, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/:project')]: wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: false }),
'/:project': wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/team')]: wrap(c.root.Team, { menuTransparency: true, footerBig: true }),
'/team': wrap(c.root.Team, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/jobs')]: wrap(c.root.Jobs, { menuTransparency: true, footerBig: true }),
'/jobs': wrap(c.root.Jobs, { menuTransparency: true, footerBig: true }),
'/press': wrap(c.root.Press, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/press')]: wrap(c.root.Press, { menuTransparency: true, footerBig: true }),
'/projects/:project_id/surveys': Wrap(c.root.Surveys, { menuTransparency: false, footerBig: false, menuShort: true }),
'/projects/:project_id/fiscal': Wrap(c.root.ProjectsFiscal, { menuTransparency: false, footerBig: false, menuShort: true }),
'/projects/:project_id/posts': Wrap(c.root.Posts, { menuTransparency: false, footerBig: false }),
'/projects/:project_id/posts/:post_id': Wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: true }),
[urlWithLocale('/projects/:project_id/posts')]: Wrap(c.root.Posts, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/projects/:project_id/posts/:post_id')]: Wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: true }),
'/projects/:project_id': Wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: false }),
'/users/:user_id': Wrap(c.root.UsersShow, { menuTransparency: true, footerBig: false }),
[urlWithLocale('/users/:user_id')]: Wrap(c.root.UsersShow, { menuTransparency: true, footerBig: false }),
'/contributions/:contribution_id/surveys/:survey_id': Wrap(c.root.SurveysShow, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/contributions/:contribution_id/surveys/:survey_id')]: Wrap(c.root.SurveysShow, { menuTransparency: false, footerBig: false }),
'/users/:user_id/edit': Wrap(c.root.UsersEdit, { menuTransparency: true, footerBig: false }),
[urlWithLocale('/users/:user_id/edit')]: Wrap(c.root.UsersEdit, { menuTransparency: true, footerBig: false }),
'/projects/:project_id/edit': Wrap(c.root.ProjectEdit, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/projects/:project_id/edit')]: Wrap(c.root.ProjectEdit, { menuTransparency: false, hideFooter: true, menuShort: true }),
'/projects/:project_id/rewards/:reward_id/surveys/new': Wrap(c.root.SurveyCreate, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/follow-fb-friends')]: Wrap(c.root.FollowFoundFriends, { menuTransparency: false, footerBig: false }),
'/follow-fb-friends': Wrap(c.root.FollowFoundFriends, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/:project')]: Wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: false }),
'/:project': Wrap(c.root.ProjectsShow, { menuTransparency: false, footerBig: false }),
[urlWithLocale('/team')]: Wrap(c.root.Team, { menuTransparency: true, footerBig: true }),
'/team': Wrap(c.root.Team, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/jobs')]: Wrap(c.root.Jobs, { menuTransparency: true, footerBig: true }),
'/jobs': Wrap(c.root.Jobs, { menuTransparency: true, footerBig: true }),
'/press': Wrap(c.root.Press, { menuTransparency: true, footerBig: true }),
[urlWithLocale('/press')]: Wrap(c.root.Press, { menuTransparency: true, footerBig: true }),

[urlWithLocale('/projects/:project_id/publish')]: wrap(c.root.Publish, { menuTransparency: false, hideFooter: true, menuShort: true }),
['/projects/:project_id/publish']: wrap(c.root.Publish, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/projects/:project_id/publish-by-steps')]: wrap(c.root.ProjectsPublishBySteps, { menuTransparency: false, hideFooter: true, menuShort: true }),
['/projects/:project_id/publish-by-steps']: wrap(c.root.ProjectsPublishBySteps, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/projects/:project_id/publish')]: Wrap(c.root.Publish, { menuTransparency: false, hideFooter: true, menuShort: true }),
['/projects/:project_id/publish']: Wrap(c.root.Publish, { menuTransparency: false, hideFooter: true, menuShort: true }),
[urlWithLocale('/projects/:project_id/publish-by-steps')]: Wrap(c.root.ProjectsPublishBySteps, { menuTransparency: false, hideFooter: true, menuShort: true }),
['/projects/:project_id/publish-by-steps']: Wrap(c.root.ProjectsPublishBySteps, { menuTransparency: false, hideFooter: true, menuShort: true }),
});
}
})();
1 change: 1 addition & 0 deletions legacy/src/vms/user-vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const fetchUser = (user_id, handlePromise = true, customProp = currentUser) => {
if (!handlePromise) {
return lUser.load();
} else {
customProp(currentUser()); // first load user from cache
lUser
.load()
.then(
Expand Down
Loading

0 comments on commit fa47bf0

Please sign in to comment.