Skip to content

Commit

Permalink
fix: Show enable button in components/flags for activated users only (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RulaKhaled authored Aug 26, 2024
1 parent 6032eb0 commit 703b34b
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const mockErroredUploads = {

const mockRepoSettingsTeamData = (isPrivate = false) => ({
owner: {
isCurrentUserPartOfOrg: null,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CommitCoverageTabs from './CommitCoverageTabs'

const mockRepoSettings = (isPrivate) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'main',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const mockProTier = {

const mockRepoSettings = (isPrivate: boolean) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down
1 change: 1 addition & 0 deletions src/pages/CommitDetailPage/Header/Header.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const mockedUseFlags = useFlags as jest.Mock<{ multipleTiers: boolean }>

const mockRepoSettings = (isPrivate = false) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down
1 change: 1 addition & 0 deletions src/pages/PullRequestPage/Header/Header.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const mockedUseFlags = useFlags as jest.Mock<{ multipleTiers: boolean }>

const mockRepoSettings = (isPrivate = false) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('Summary', () => {
ctx.status(200),
ctx.data({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const mockTreeData = {

const mockRepoSettings = {
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
activated: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('FilesChangedTab', () => {
ctx.status(200),
ctx.data({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
activated: true,
Expand Down
1 change: 1 addition & 0 deletions src/pages/RepoPage/CommitsTab/CommitsTab.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const mockBranch = (branchName) => ({

const mockRepoSettings = (isPrivate = false) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
activated: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('TokensTeam', () => {
ctx.status(200),
ctx.data({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
activated: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ jest.mock('./Header', () => ({ children }: { children: React.ReactNode }) => (
<p>Components Header Component {children}</p>
))

const mockRepoSettings = (isPrivate = false) => ({
const mockRepoSettings = (
isPrivate = false,
isCurrentUserPartOfOrg = true
) => ({
owner: {
isCurrentUserPartOfOrg,
repository: {
__typename: 'Repository',
activated: true,
Expand Down Expand Up @@ -154,11 +158,13 @@ describe('Components Tab', () => {
flags = [nextPageFlagData, initialFlagData],
tierValue = TierNames.PRO,
isPrivate = false,
isCurrentUserPartOfOrg = true,
}: {
data?: object
flags?: any[]
tierValue?: TTierNames
isPrivate?: boolean
isCurrentUserPartOfOrg?: boolean
}) {
server.use(
graphql.query('OwnerTier', (req, res, ctx) => {
Expand All @@ -174,7 +180,10 @@ describe('Components Tab', () => {
)
}),
graphql.query('GetRepoSettingsTeam', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(mockRepoSettings(isPrivate)))
return res(
ctx.status(200),
ctx.data(mockRepoSettings(isPrivate, isCurrentUserPartOfOrg))
)
}),
graphql.query('BackfillComponentMemberships', (req, res, ctx) =>
res(ctx.status(200), ctx.data(data))
Expand Down Expand Up @@ -328,4 +337,32 @@ describe('Components Tab', () => {
expect(enableText).toBeInTheDocument()
})
})

describe('when current user is not part of org and data is not enabled', () => {
beforeEach(() => {
setup({
data: {
config: {
isTimescaleEnabled: true,
},
owner: {
repository: {
__typename: 'Repository',
componentsMeasurementsActive: false,
componentsMeasurementsBackfilled: false,
},
},
},
isCurrentUserPartOfOrg: false,
})
})

it('renders empty state message', async () => {
render(<ComponentsTab />, { wrapper })
const componentsText = await screen.findByText(
/Component analytics is disabled./
)
expect(componentsText).toBeInTheDocument()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ function ComponentsTab() {
})
}
>
<BackfillBanners />
{repoSettings?.isCurrentUserPartOfOrg ? (
<BackfillBanners />
) : (
<div className="mt-3 text-center">
<hr />
<p className="mt-4 p-3">Component analytics is disabled.</p>
</div>
)}
</Header>
<div className="flex flex-1 flex-col gap-4">
{showComponentsTable({
Expand Down
1 change: 1 addition & 0 deletions src/pages/RepoPage/CoverageTab/CoverageTab.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CoverageTab from './CoverageTab'

const mockRepoSettingsTeam = {
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down
9 changes: 8 additions & 1 deletion src/pages/RepoPage/CoverageTab/FlagsTab/FlagsTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ function FlagsTab() {
isRepoBackfilling,
})}
>
<BackfillBanners />
{repoSettings?.isCurrentUserPartOfOrg ? (
<BackfillBanners />
) : (
<div className="mt-3 text-center">
<hr />
<p className="mt-4 p-3">Flag analytics is disabled.</p>
</div>
)}
</Header>
<div className="flex flex-1 flex-col gap-4">
{showFlagsTable({
Expand Down
41 changes: 39 additions & 2 deletions src/pages/RepoPage/CoverageTab/FlagsTab/FlagsTab.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ const flagsData = [
},
]

const mockRepoSettings = (isPrivate = false) => ({
const mockRepoSettings = (
isPrivate = false,
isCurrentUserPartOfOrg = true
) => ({
owner: {
isCurrentUserPartOfOrg,
repository: {
__typename: 'Repository',
activated: true,
Expand Down Expand Up @@ -94,6 +98,7 @@ describe('Flags Tab', () => {
flags = flagsData,
tierValue = TierNames.PRO,
isPrivate = false,
isCurrentUserPartOfOrg = true,
}) {
useRepoFlagsSelect.mockReturnValue({ data: flags })
useRepoBackfilled.mockReturnValue(data)
Expand All @@ -112,7 +117,10 @@ describe('Flags Tab', () => {
)
}),
graphql.query('GetRepoSettingsTeam', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(mockRepoSettings(isPrivate)))
return res(
ctx.status(200),
ctx.data(mockRepoSettings(isPrivate, isCurrentUserPartOfOrg))
)
})
)
}
Expand Down Expand Up @@ -321,4 +329,33 @@ describe('Flags Tab', () => {
expect(enableText).toBeInTheDocument()
})
})

describe('when current user is not part of org and data is not enabled', () => {
beforeEach(() => {
setup({
data: {
data: {
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
isTimescaleEnabled: true,
},
},
flags: [
{
name: 'flag1',
},
{
name: 'flag2',
},
],
isCurrentUserPartOfOrg: false,
})
})

it('renders empty state message', async () => {
render(<FlagsTab />, { wrapper })
const flagsText = await screen.findByText(/Flag analytics is disabled./)
expect(flagsText).toBeInTheDocument()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jest.mock('./subroute/Fileviewer', () => () => 'FileViewer')

const mockRepoSettings = (isPrivate = false) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
defaultBranch: 'master',
private: isPrivate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('react-use/lib/useIntersection')

const mockRepoSettings = (isPrivate) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
activated: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jest.mock('ui/CodeRenderer/hooks/useScrollToLine')

const mockRepoSettings = (isPrivate) => ({
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
activated: true,
Expand Down
33 changes: 4 additions & 29 deletions src/services/repo/useRepoSettingsTeam.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,17 @@ afterAll(() => server.close())

const mockNotFoundError = {
owner: {
isCurrentUserPartOfOrg: null,
repository: {
__typename: 'NotFoundError',
message: 'repo not found',
},
},
}

const mockOwnerNotActivatedError = {
owner: {
repository: {
__typename: 'OwnerNotActivatedError',
message: 'owner not activated',
},
},
}

const mockIncorrectResponse = {
owner: {
isCurrentUserPartOfOrg: false,
repository: {
invalid: 'invalid',
},
Expand All @@ -57,6 +50,7 @@ const mockIncorrectResponse = {

const mockResponse = {
owner: {
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand All @@ -75,15 +69,12 @@ const mockResponse = {
describe('useRepoSettingsTeam', () => {
function setup({
isNotFoundError = false,
isOwnerNotActivatedError = false,
isUnsuccessfulParseError = false,
}) {
server.use(
graphql.query('GetRepoSettingsTeam', (req, res, ctx) => {
if (isNotFoundError) {
return res(ctx.status(200), ctx.data(mockNotFoundError))
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivatedError))
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockIncorrectResponse))
}
Expand All @@ -109,6 +100,7 @@ describe('useRepoSettingsTeam', () => {

await waitFor(() =>
expect(result.current.data).toEqual({
isCurrentUserPartOfOrg: true,
repository: {
__typename: 'Repository',
defaultBranch: 'master',
Expand Down Expand Up @@ -162,21 +154,4 @@ describe('useRepoSettingsTeam', () => {
)
})
})

describe('when owner is not activated', () => {
it('returns an owner not activated error', async () => {
setup({ isOwnerNotActivatedError: true })
const { result } = renderHook(() => useRepoSettingsTeam(), {
wrapper,
})
await waitFor(() => expect(result.current.isError).toBeTruthy())
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 403,
})
)
)
})
})
})
Loading

0 comments on commit 703b34b

Please sign in to comment.