Skip to content

Commit

Permalink
fix: Hide tokenless banner for signed out user
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin-codecov committed Jan 24, 2025
1 parent 02d5b6b commit ef1d10b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { setupServer } from 'msw/node'
import { Suspense } from 'react'
import { MemoryRouter, Route } from 'react-router-dom'

import { Plans } from 'shared/utils/billing'

import TokenlessBanner from './TokenlessBanner'

const mocks = vi.hoisted(() => ({
Expand All @@ -15,6 +17,8 @@ vi.mock('shared/featureFlags', () => ({
useFlags: mocks.useFlags,
}))

vi.mock('services/users')

vi.mock('./TokenRequiredBanner', () => ({
default: () => 'TokenRequiredBanner',
}))
Expand All @@ -41,6 +45,50 @@ afterAll(() => {
server.close()
})

const mockSignedInUser = {
me: {
owner: {
defaultOrgUsername: 'codecov',
},
email: '[email protected]',
privateAccess: true,
onboardingCompleted: true,
businessEmail: '[email protected]',
termsAgreement: true,
user: {
name: 'Jane Doe',
username: 'janedoe',
avatarUrl: 'http://127.0.0.1/avatar-url',
avatar: 'http://127.0.0.1/avatar-url',
student: false,
studentCreatedAt: null,
studentUpdatedAt: null,
customerIntent: 'PERSONAL',
},
trackingMetadata: {
service: 'github',
ownerid: 123,
serviceId: '123',
plan: Plans.USERS_BASIC,
staff: false,
hasYaml: false,
bot: null,
delinquent: null,
didTrial: null,
planProvider: null,
planUserCount: 1,
createdAt: 'timestamp',
updatedAt: 'timestamp',
profile: {
createdAt: 'timestamp',
otherGoal: null,
typeProjects: [],
goals: [],
},
},
},
}

const wrapper =
(initialEntries = ['/gh/codecov']): React.FC<React.PropsWithChildren> =>
({ children }) => (
Expand All @@ -57,9 +105,11 @@ describe('TokenlessBanner', () => {
function setup({
tokenlessSection = true,
uploadTokenRequired = false,
currentUser,
}: {
tokenlessSection?: boolean
uploadTokenRequired?: boolean
currentUser?: any
} = {}) {
mocks.useFlags.mockReturnValue({ tokenlessSection })

Expand All @@ -74,12 +124,15 @@ describe('TokenlessBanner', () => {
},
},
})
}),
graphql.query('CurrentUser', () => {
return HttpResponse.json({ data: currentUser })
})
)
}

it('renders nothing when tokenlessSection flag is false', () => {
setup({ tokenlessSection: false })
setup({ tokenlessSection: false, currentUser: mockSignedInUser })
const { container } = render(<TokenlessBanner />, { wrapper: wrapper() })
expect(container).toBeEmptyDOMElement()
})
Expand All @@ -93,7 +146,7 @@ describe('TokenlessBanner', () => {
})

it('renders TokenRequiredBanner when uploadTokenRequired is true', async () => {
setup({ uploadTokenRequired: true })
setup({ uploadTokenRequired: true, currentUser: mockSignedInUser })
render(<TokenlessBanner />, { wrapper: wrapper() })

await waitFor(() => {
Expand All @@ -102,13 +155,19 @@ describe('TokenlessBanner', () => {
})
})

it('renders TokenNotRequiredBanner when uploadTokenRequired is false', async () => {
setup({ uploadTokenRequired: false })
it.only('renders TokenNotRequiredBanner when uploadTokenRequired is false', async () => {
setup({ uploadTokenRequired: false, currentUser: mockSignedInUser })
render(<TokenlessBanner />, { wrapper: wrapper() })

await waitFor(() => {
const banner = screen.getByText('TokenNotRequiredBanner')
expect(banner).toBeInTheDocument()
})
})

it.only('renders nothing when currentUser is not provided', () => {
setup({ uploadTokenRequired: false, currentUser: mockSignedInUser })
const { container } = render(<TokenlessBanner />, { wrapper: wrapper() })
expect(container).toBeEmptyDOMElement()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { lazy } from 'react'
import { useParams } from 'react-router-dom'

import { useUploadTokenRequired } from 'services/uploadTokenRequired'
import { useUser } from 'services/user'
import { useFlags } from 'shared/featureFlags'

const TokenRequiredBanner = lazy(() => import('./TokenRequiredBanner'))
Expand All @@ -18,8 +19,9 @@ const TokenlessBanner: React.FC = () => {
})
const { provider, owner } = useParams<UseParams>()
const { data } = useUploadTokenRequired({ provider, owner, enabled: !!owner })
const { data: currentUser } = useUser()

if (!tokenlessSection || !owner || !data) return null
if (!tokenlessSection || !owner || !data || !currentUser?.user) return null

return data?.uploadTokenRequired ? (
<TokenRequiredBanner />
Expand Down

0 comments on commit ef1d10b

Please sign in to comment.