-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(content-sidebar): Fix Box AI Sidebar Title render logic (#3902)
* fix(content-sidebar): Fix Box AI Sidebar Title render logic * fix(content-sidebar): Fix Box AI Sidebar Title render logic
- Loading branch information
1 parent
fcf335a
commit 9135321
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @file Box AI sidebar title component | ||
* @author Box | ||
*/ | ||
import * as React from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
import { useAgents, REQUEST_STATE } from '@box/box-ai-agent-selector'; | ||
import { Text } from '@box/blueprint-web'; | ||
|
||
import messages from '../common/messages'; | ||
|
||
interface BoxAISidebarTitleProps { | ||
isAIStudioAgentSelectorEnabled?: boolean; | ||
} | ||
|
||
function BoxAISidebarTitle({ isAIStudioAgentSelectorEnabled = false }: BoxAISidebarTitleProps) { | ||
const { formatMessage } = useIntl(); | ||
|
||
const { agents, requestState } = useAgents(); | ||
|
||
const hasAgentSelectorRequestEnded = | ||
requestState !== REQUEST_STATE.NOT_STARTED && requestState !== REQUEST_STATE.IN_PROGRESS; | ||
const hasAgentSelectorRequestFailed = | ||
requestState === REQUEST_STATE.ERROR || requestState === REQUEST_STATE.CANCELLED; | ||
const isAgentSelectorVisible = agents.length > 1 && requestState === REQUEST_STATE.SUCCESS; | ||
|
||
// We want to display the title when the agent selector is disabled | ||
// or when the request has failed | ||
// or when the request has succeeded and the agent selector is not visible | ||
return !isAIStudioAgentSelectorEnabled || | ||
hasAgentSelectorRequestFailed || | ||
(!isAgentSelectorVisible && hasAgentSelectorRequestEnded) ? ( | ||
<Text as="h3" className="bcs-title"> | ||
{formatMessage(messages.sidebarBoxAITitle)} | ||
</Text> | ||
) : null; | ||
} | ||
|
||
export default BoxAISidebarTitle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/elements/content-sidebar/__tests__/BoxAISidebarTitle.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
import { REQUEST_STATE, useAgents as originalUseAgents } from '@box/box-ai-agent-selector'; | ||
import { render, screen } from '../../../test-utils/testing-library'; | ||
import BoxAISidebarTitle from '../BoxAISidebarTitle'; | ||
|
||
jest.mock('@box/box-ai-agent-selector', () => { | ||
const actualModule = jest.requireActual('@box/box-ai-agent-selector'); | ||
return { | ||
...actualModule, | ||
useAgents: jest.fn(), | ||
}; | ||
}); | ||
|
||
const useAgents = originalUseAgents as jest.MockedFunction<typeof originalUseAgents>; | ||
|
||
const renderComponent = props => { | ||
return render(<BoxAISidebarTitle {...props} />); | ||
}; | ||
|
||
describe('elements/content-sidebar/BoxAISidebarTitle', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('given isAIStudioAgentSelectorEnabled is false, should render title', () => { | ||
useAgents.mockReturnValue({ agents: [], requestState: REQUEST_STATE.NOT_STARTED, selectedAgent: null }); | ||
renderComponent({ isAIStudioAgentSelectorEnabled: false }); | ||
expect(screen.getByText('Box AI')).toBeInTheDocument(); | ||
}); | ||
|
||
describe('given isAIStudioAgentSelectorEnabled is true', () => { | ||
test.each` | ||
agentsData | condition | ||
${{ agents: [], requestState: REQUEST_STATE.ERROR, selectedAgent: null }} | ${'agent selector request has failed'} | ||
${{ agents: [], requestState: REQUEST_STATE.CANCELLED, selectedAgent: null }} | ${'agent selector request has been cancelled'} | ||
${{ agents: [{ id: '123', name: 'Agent 1' }], requestState: REQUEST_STATE.SUCCESS, selectedAgent: { id: '123', name: 'Agent 1' } }} | ${'agent selector request is successful and there is 1 agent available'} | ||
`('and $condition, should render title', ({ agentsData }) => { | ||
useAgents.mockReturnValue(agentsData); | ||
renderComponent({ isAIStudioAgentSelectorEnabled: true }); | ||
expect(screen.getByText('Box AI')).toBeInTheDocument(); | ||
}); | ||
|
||
test.each` | ||
agentsData | condition | ||
${{ agents: [], requestState: REQUEST_STATE.NOT_STARTED, selectedAgent: null }} | ${'agent selector has not started loading'} | ||
${{ agents: [], requestState: REQUEST_STATE.IN_PROGRESS, selectedAgent: null }} | ${'agent selector request is in progress'} | ||
${{ agents: [{ id: '123', name: 'Agent 1' }, { id: '124', name: 'Agent 2' }], requestState: REQUEST_STATE.SUCCESS, selectedAgent: { id: '123', name: 'Agent 1' } }} | ${'agent selector request is successful and 2 agents are available'} | ||
`('and $condition, should not render title', ({ agentsData }) => { | ||
useAgents.mockReturnValue(agentsData); | ||
renderComponent({ isAIStudioAgentSelectorEnabled: true }); | ||
expect(screen.queryByText('Box AI')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |