Skip to content

Commit

Permalink
feat: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meelrossi committed May 16, 2024
1 parent 4b92d85 commit 1c08ba2
Show file tree
Hide file tree
Showing 21 changed files with 720 additions and 125 deletions.
3 changes: 2 additions & 1 deletion src/components/WorldListPage/NameTabs/NameTabs.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ describe('when isWorldContributorEnabled is false', () => {

onNavigate = jest.fn()
})
it('should not show contributor tab', () => {

it('should not show the contributor tab', () => {
render(<NameTabs onNavigate={onNavigate} isWorldContributorEnabled={false} />)
expect(screen.getByText(dclTabText)).toBeInTheDocument()
expect(screen.getByText(ensTabText)).toBeInTheDocument()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { t } from 'decentraland-dapps/dist/modules/translation'
import { ENS } from 'modules/ens/types'
import { Deployment } from 'modules/deployment/types'
import { Project } from 'modules/project/types'
import { Props } from './PublishSceneButton.types'
import PublishSceneButton from './PublishSceneButton'

const ens = {
name: 'test',
subdomain: 'test',
content: '',
ensOwnerAddress: '0xtest1',
nftOwnerAddress: '0xtest1',
resolver: '0xtest3',
tokenId: '',
ensAddressRecord: '',
worldStatus: {
healthy: true
}
} as ENS

let deploymentsByWorlds: Record<string, Deployment>
let projects: Project[]

function renderPublishSceneButton(props: Partial<Props>) {
return render(
<PublishSceneButton
deploymentsByWorlds={{}}
ens={ens}
projects={[]}
onEditScene={jest.fn()}
onPublishScene={jest.fn()}
onUnpublishScene={jest.fn()}
{...props}
/>
)
}

describe('when the world has a scene deployed', () => {
beforeEach(() => {
deploymentsByWorlds = {
[ens.subdomain]: {
projectId: '1',
name: 'Deployment'
} as Deployment
}
})

describe('and the user has access to the deployed project', () => {
beforeEach(() => {
projects = [{ id: '1' } as Project]
})

it('should show edit scene button', () => {
const screen = renderPublishSceneButton({ projects, deploymentsByWorlds })
expect(screen.getByRole('button', { name: t('worlds_list_page.table.edit_scene') })).toBeInTheDocument()
})

describe('when editScene button is clicked', () => {
it('should trigger onEditScene callback action', () => {
const onEditScene = jest.fn()
const screen = renderPublishSceneButton({ onEditScene, projects, deploymentsByWorlds })
const editSceneButton = screen.getByRole('button', { name: t('worlds_list_page.table.edit_scene') })
userEvent.click(editSceneButton)
expect(onEditScene).toHaveBeenCalled()
})
})
})

describe("and the user doesn't have access to the deployed project", () => {
beforeEach(() => {
projects = []
})

it('should show unpublish scene button', () => {
const screen = renderPublishSceneButton({ projects, deploymentsByWorlds })
expect(screen.getByRole('button', { name: t('worlds_list_page.table.unpublish_scene') })).toBeInTheDocument()
})

describe('when unpublish button is clicked', () => {
it('should trigger onUnpublish callback action', () => {
const onUnpublishScene = jest.fn()
const screen = renderPublishSceneButton({ onUnpublishScene, projects, deploymentsByWorlds })
const unpublishSceneButton = screen.getByRole('button', { name: t('worlds_list_page.table.unpublish_scene') })
userEvent.click(unpublishSceneButton)
expect(onUnpublishScene).toHaveBeenCalled()
})
})
})
})

describe('when the world has no scene deployed', () => {
it('should show publish scene button', () => {
const screen = renderPublishSceneButton({})
expect(screen.getByRole('button', { name: t('worlds_list_page.table.publish_scene') })).toBeInTheDocument()
})

describe('when publish button is clicked', () => {
it('should trigger onPublish callback action', () => {
const onPublishScene = jest.fn()
const screen = renderPublishSceneButton({ onPublishScene })
const publishButton = screen.getByRole('button', { name: t('worlds_list_page.table.publish_scene') })
userEvent.click(publishButton)
expect(onPublishScene).toHaveBeenCalled()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Button, Popup } from 'decentraland-ui'
import { Props } from './PublishSceneButton.types'
import { isWorldDeployed } from '../utils'
import { t } from 'decentraland-dapps/dist/modules/translation'

export default function PublishSceneButton({
deploymentsByWorlds,
ens,
projects,
onEditScene,
onUnpublishScene,
onPublishScene
}: Props): JSX.Element {
const deployment = deploymentsByWorlds[ens.subdomain]
return isWorldDeployed(deploymentsByWorlds, ens) ? (
<div className="publish-scene">
<Popup content={deployment?.name} on="hover" trigger={<span>{deployment?.name}</span>} />
{projects.find(project => project.id === deployment?.projectId)
? onEditScene && (
<Button inverted size="small" onClick={() => onEditScene(ens)}>
{t('worlds_list_page.table.edit_scene')}
</Button>
)
: onUnpublishScene && (
<Popup
content={t('worlds_list_page.table.scene_published_outside_builder')}
on="hover"
position="top center"
trigger={
<Button inverted size="small" onClick={() => onUnpublishScene(ens)}>
{t('worlds_list_page.table.unpublish_scene')}
</Button>
}
/>
)}
</div>
) : (
<div className="publish-scene">
<span>-</span>
{onPublishScene && (
<Button primary size="small" onClick={onPublishScene}>
{t('worlds_list_page.table.publish_scene')}
</Button>
)}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Deployment } from 'modules/deployment/types'
import { ENS } from 'modules/ens/types'
import { Project } from 'modules/project/types'

export type Props = {
deploymentsByWorlds: Record<string, Deployment>
ens: ENS
projects: Project[]
onEditScene?: (ens: ENS) => void
onUnpublishScene?: (ens: ENS) => void
onPublishScene?: () => void
}
3 changes: 3 additions & 0 deletions src/components/WorldListPage/PublishSceneButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import PublishSceneButton from './PublishSceneButton'

export default PublishSceneButton
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { t } from 'decentraland-dapps/dist/modules/translation'
import { renderWithProviders } from 'specs/utils'
import { Deployment } from 'modules/deployment/types'
import { ENS } from 'modules/ens/types'
import { fromBytesToMegabytes, getExplorerUrl } from '../utils'
import WorldContributorTab from './WorldContributorTab'
import { Props } from './WorldContributorTab.types'
import userEvent from '@testing-library/user-event'

export function renderWorldContributorTab(props: Partial<Props>) {
return renderWithProviders(
<WorldContributorTab
items={[]}
deploymentsByWorlds={{}}
error={null}
loading={false}
onNavigate={jest.fn()}
onUnpublishWorld={jest.fn()}
projects={[]}
{...props}
/>
)
}

describe('when loading the items', () => {
it('should show spinner', () => {
const screen = renderWorldContributorTab({ loading: true })
expect(screen.container.getElementsByClassName('loader').length).toBe(1)
})
})

describe('when rendering contributable names table', () => {
let deploymentsByWorlds: Record<string, Deployment>
let items: ENS[]

describe('when the user has no contributable names', () => {
beforeEach(() => {
items = []
})

it('should show empty placeholder', () => {
const screen = renderWorldContributorTab({ items })
expect(screen.getByText(t('worlds_list_page.empty_contributor_list.title')))
})
})

describe('when the user has contributable names', () => {
beforeEach(() => {
items = [
{
name: 'test.dcl.eth',
subdomain: 'test.dcl.eth',
content: '',
ensOwnerAddress: '0xtest1',
nftOwnerAddress: '0xtest1',
resolver: '0xtest3',
tokenId: '',
ensAddressRecord: '',
size: '1048576',
userPermissions: ['deployment'],
worldStatus: {
healthy: true,
scene: { urn: 'urn', entityId: 'entityId' }
}
}
]
})

it("should show item's name", () => {
const screen = renderWorldContributorTab({ items })
expect(screen.getByText(items[0].name)).toBeInTheDocument()
})

it("should show item's size", () => {
const screen = renderWorldContributorTab({ items })
expect(screen.getByText(fromBytesToMegabytes(Number(items[0].size!)))).toBeInTheDocument()
})

it("should show item's permissions", () => {
const screen = renderWorldContributorTab({ items })
expect(screen.getByText(t(`worlds_list_page.table.user_permissions.${items[0].userPermissions![0]}`))).toBeInTheDocument()
})

describe('when the world has a scene deployed', () => {
beforeEach(() => {
deploymentsByWorlds = {
['test.dcl.eth']: {
projectId: '1',
id: '1'
} as Deployment
}
})

it("should show world's url", () => {
const screen = renderWorldContributorTab({ items, deploymentsByWorlds })
expect(screen.getByText(getExplorerUrl(items[0].subdomain)))
})

describe('and the user has deployment permissions', () => {
beforeEach(() => {
items = [
{
...items[0],
userPermissions: ['deployment']
}
]
})

it('should show unpublish scene button', () => {
const screen = renderWorldContributorTab({ items, deploymentsByWorlds })
expect(screen.getByRole('button', { name: t('worlds_list_page.table.unpublish_scene') })).toBeInTheDocument()
})

it('should trigger onUnpublishWorld action when unpublish button is clicked', () => {
const onUnpublishWorld = jest.fn()
const screen = renderWorldContributorTab({ items, deploymentsByWorlds, onUnpublishWorld })
const unpublishBtn = screen.getByRole('button', { name: t('worlds_list_page.table.unpublish_scene') })
userEvent.click(unpublishBtn)
expect(onUnpublishWorld).toHaveBeenCalled()
})
})

describe("and the user doesn't have deployment permissions", () => {
beforeEach(() => {
items = [
{
...items[0],
userPermissions: ['streaming']
}
]
})

it('should not show unpublish scene button', () => {
const screen = renderWorldContributorTab({ items, deploymentsByWorlds })
expect(screen.queryByRole('button', { name: t('worlds_list_page.table.unpublish_scene') })).not.toBeInTheDocument()
})
})
})

describe('when the world has no scene deployed', () => {
beforeEach(() => {
deploymentsByWorlds = {}
})

describe('and the user has deployment permissions', () => {
beforeEach(() => {
items = [
{
...items[0],
userPermissions: ['deployment']
}
]
})

it('should show publish scene button', () => {
const screen = renderWorldContributorTab({ items, deploymentsByWorlds })
expect(screen.getByRole('button', { name: t('worlds_list_page.table.publish_scene') })).toBeInTheDocument()
})
})

describe("and the user doesn't have deployment permissions", () => {
beforeEach(() => {
items = [
{
...items[0],
userPermissions: ['streaming']
}
]
})

it('should not show publish scene button', () => {
const screen = renderWorldContributorTab({ items, deploymentsByWorlds })
expect(screen.queryByRole('button', { name: t('worlds_list_page.table.publish_scene') })).not.toBeInTheDocument()
})
})
})
})
})
Loading

0 comments on commit 1c08ba2

Please sign in to comment.