Skip to content

Commit

Permalink
editor and topbar tests
Browse files Browse the repository at this point in the history
Former-commit-id: 7331382
  • Loading branch information
horacioh committed May 6, 2022
1 parent 637e6bf commit 09c9c83
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 35 deletions.
79 changes: 79 additions & 0 deletions frontend/app/src/components/__tests__/topbar.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {Account, Document} from '@app/client'
import {queryKeys} from '@app/hooks'
import {MainPageProviders, mountWithAccount} from '@app/test/utils'
import {Topbar} from '@components/topbar'

describe('Topbar', () => {
it('default render', () => {
let {render, client} = mountWithAccount()
render(
<MainPageProviders client={client}>
<Topbar />
</MainPageProviders>,
)
})

it('render Draft Title and Author', () => {
let {render, client} = mountWithAccount()
let date = new Date()

let draft: Document = {
id: 'foo',
title: 'test demo',
subtitle: 'test subtitle',
author: 'authortest',
content: '',
updateTime: date,
createTime: date,
publishTime: date,
children: [],
}

let author: Account = {
id: 'authortest',
profile: {
alias: 'test demo user',
email: '[email protected]',
bio: 'demo',
},
devices: {
d1: {
peerId: 'd1',
},
},
}

client.setQueryData<Document>([queryKeys.GET_DRAFT, 'foo'], draft)

client.setQueryData<Account>([queryKeys.GET_ACCOUNT, 'authortest'], author)

render(
<MainPageProviders client={client} mainPageContext={{document: draft}}>
<Topbar />
</MainPageProviders>,
)
.get('[data-testid="topbar-title"]')
.contains(draft.title)
.get('[data-testid="topbar-author"]')
.contains(author.profile!.alias)
})

it('navigation button should work', () => {
let {render, client} = mountWithAccount()
let back = cy.stub()
let forward = cy.stub()
render(
<MainPageProviders client={client}>
<Topbar back={back} forward={forward} />
</MainPageProviders>,
)
.get("[data-testid='history-back']")
.click()
.get("[data-testid='history-forward']")
.click()
.then(() => {
expect(back).to.have.been.calledOnce
expect(forward).to.have.been.calledOnce
})
})
})
61 changes: 49 additions & 12 deletions frontend/app/src/components/topbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {getTitleFromContent} from '@app/editor/use-editor-draft'
import {useFiles, useMainPage} from '@app/main-page-context'
import {useAccount} from '@app/hooks'
import {useMainPage} from '@app/main-page-context'
import {css, styled} from '@app/stitches.config'
import {Text} from '@components/text'
import {invoke} from '@tauri-apps/api'
Expand Down Expand Up @@ -46,10 +47,18 @@ export const topbarSection = css({
alignItems: 'center',
})

export function Topbar() {
type TopbarProps = {
back?: () => void
forward: () => void
}

export function Topbar({
back = window.history.back,
forward = window.history.forward,
}: TopbarProps) {
let mainPage = useMainPage()
let [mainState] = useActor(mainPage)
let files = useFiles()
let {data, isSuccess} = useAccount(mainState.context.document?.author)

async function onCreateDraft() {
await invoke('open_in_new_window', {url: '/new'})
Expand All @@ -59,7 +68,7 @@ export function Topbar() {
mainState.context.library.send('LIBRARY.TOGGLE')
}

let title = getDocumentTitle(mainState.context.currentDocument)
let title = getDocumentTitle(mainState.context.document)
console.log('Current Location: ', window.location.pathname)

return (
Expand All @@ -68,27 +77,55 @@ export function Topbar() {
<Box css={{display: 'flex'}} {...draggableProps}>
<TopbarButton
color="muted"
data-testid="history-back"
onClick={(e) => {
e.preventDefault()
window.history.back()
back()
}}
>
<Icon name="ArrowChevronLeft" color="muted" size="2" />
</TopbarButton>
<TopbarButton
color="muted"
data-testid="history-forward"
onClick={(e) => {
e.preventDefault()
window.history.forward()
forward()
}}
>
<Icon name="ArrowChevronRight" color="muted" size="2" />
</TopbarButton>
</Box>
<Box css={{flex: 1}} data-tauri-drag-region>
<Text size="3" fontWeight="medium" data-tauri-drag-region>
<Box
css={{flex: 1, display: 'flex', alignItems: 'baseline', gap: '$2'}}
data-tauri-drag-region
>
<Text
size="3"
fontWeight="medium"
aria-label="Document Title"
data-testid="topbar-title"
data-tauri-drag-region
>
{title}
</Text>
<Text size="1" color="muted">
by
</Text>
{data && isSuccess ? (
<Text
size="1"
color="muted"
css={{textDecoration: 'underline'}}
data-testid="topbar-author"
>
{data.profile?.alias}
</Text>
) : (
<Text size="1" color="muted" css={{textDecoration: 'underline'}}>
...
</Text>
)}
</Box>
{/* <Box>other actions</Box> */}
<TopbarButton onClick={toggleLibrary} data-tauri-drag-region>
Expand All @@ -111,12 +148,12 @@ export function Topbar() {
)
}

function getDocumentTitle(currentDocument: any) {
let titleText = currentDocument?.content
function getDocumentTitle(document: any) {
let titleText = document?.content
? getTitleFromContent({
children: currentDocument.content,
children: document.content,
})
: currentDocument?.title ?? ''
: document?.title ?? ''

return titleText.length < 50 ? titleText : `${titleText.substring(0, 49)}...`
}
8 changes: 4 additions & 4 deletions frontend/app/src/main-page-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type MainPageContext = {
version: string | null
blockId: string | null
}
currentDocument: Document | null
document: Document | null
files: ActorRefFrom<ReturnType<typeof createFilesMachine>>
drafts: ActorRefFrom<ReturnType<typeof createDraftsMachine>>
library: ActorRefFrom<typeof libraryMachine>
Expand Down Expand Up @@ -212,7 +212,7 @@ export function createMainPageMachine(client: QueryClient) {
version: null,
blockId: null,
},
currentDocument: null,
document: null,
files: spawn(createFilesMachine(client), 'files'),
drafts: spawn(createDraftsMachine(client), 'drafts'),
library: spawn(libraryMachine, 'library'),
Expand Down Expand Up @@ -369,10 +369,10 @@ export function createMainPageMachine(client: QueryClient) {
context.drafts.send('RECONCILE')
},
setCurrentDocument: assign({
currentDocument: (_, event) => event.document
document: (_, event) => event.document
}),
clearCurrentDocument: assign({
currentDocument: (c) => null
document: (c) => null
}),
setDraftParams: assign({
params: (_, e, meta) => {
Expand Down
11 changes: 8 additions & 3 deletions frontend/app/src/pages/__tests__/editor-page.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Editor Page', () => {
},
}

client.setQueryData<Document>([queryKeys.GET_DRAFT, 'foo'], {
let draft: Document = {
id: 'foo',
title: 'test demo',
subtitle: 'test subtitle',
Expand All @@ -36,7 +36,9 @@ describe('Editor Page', () => {
createTime: date,
publishTime: date,
children: [],
})
}

client.setQueryData<Document>([queryKeys.GET_DRAFT, 'foo'], draft)

client.setQueryData<Publication>(
[queryKeys.GET_PUBLICATION, 'foo', 1],
Expand All @@ -63,7 +65,10 @@ describe('Editor Page', () => {
render(
<MainPageProviders
client={client}
mainPageContext={{params: {docId: 'foo', blockId: 'block'}}}
mainPageContext={{
params: {docId: 'foo', blockId: 'block', version: null},
document: draft,
}}
>
<EditorPage
editor={elEditor}
Expand Down
64 changes: 48 additions & 16 deletions frontend/app/src/pages/publication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function Publication() {
}
}, [docId])

async function handleUpdate() {
async function handleEdit() {
try {
const d = await createDraft(docId)
if (d?.id) {
Expand Down Expand Up @@ -128,28 +128,60 @@ export default function Publication() {
</Text>
</Box>
<Box className={footerButtonsStyles()}>
<Button
size="1"
variant="outlined"
disabled={state.hasTag('pending')}
data-testid="submit-review"
onClick={() => {
console.log('Review: IMPLEMENT ME!')
}}
>
Review
</Button>
<Button
variant="outlined"
size="1"
disabled={state.hasTag('pending')}
data-testid="submit-reply"
data-testid="submit-edit"
onClick={() => {
console.log('Reply: IMPLEMENT ME!')
console.log('Send: IMPLEMENT ME!')
}}
>
Reply
Send
</Button>
{state.context.canUpdate ? (
<>
<Button
color="success"
size="1"
disabled={state.hasTag('pending')}
data-testid="submit-edit"
onClick={handleEdit}
>
Edit
</Button>
</>
) : (
<>
<TippingModal
publicationId={state.context.publication?.document.id}
accountId={state.context.publication?.document.author}
visible={!state.context.canUpdate}
/>
<Button
size="1"
variant="outlined"
disabled={state.hasTag('pending')}
data-testid="submit-review"
onClick={() => {
console.log('Review: IMPLEMENT ME!')
}}
>
Review
</Button>
<Button
variant="outlined"
size="1"
disabled={state.hasTag('pending')}
data-testid="submit-reply"
onClick={() => {
console.log('Reply: IMPLEMENT ME!')
}}
>
Reply
</Button>
</>
)}
</Box>
</Box>
</>
Expand Down Expand Up @@ -497,7 +529,7 @@ function TippingModal({
<PopoverPrimitive.Trigger asChild>
<Button
size="1"
variant="ghost"
variant="outlined"
color="success"
onClick={() => {
send('OPEN')
Expand Down

0 comments on commit 09c9c83

Please sign in to comment.