Path: blob/trunk/javascript/grid-ui/src/tests/components/ColumnSelector.test.tsx
2887 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617import * as React from 'react'18import ColumnSelector from '../../components/RunningSessions/ColumnSelector'19import { act, screen, within } from '@testing-library/react'20import { render } from '../utils/render-utils'21import userEvent from '@testing-library/user-event'22import '@testing-library/jest-dom'2324const localStorageMock = (() => {25let store: Record<string, string> = {}26return {27getItem: jest.fn((key: string) => store[key] || null),28setItem: jest.fn((key: string, value: string) => {29store[key] = value30}),31clear: jest.fn(() => {32store = {}33})34}35})()3637Object.defineProperty(window, 'localStorage', {38value: localStorageMock39})4041const mockSessions = [42{43id: 'session1',44capabilities: JSON.stringify({45browserName: 'chrome',46browserVersion: '88.0',47platformName: 'windows',48acceptInsecureCerts: true49})50},51{52id: 'session2',53capabilities: JSON.stringify({54browserName: 'firefox',55browserVersion: '78.0',56platformName: 'linux',57acceptSslCerts: false58})59}60]6162beforeEach(() => {63localStorageMock.clear()64jest.clearAllMocks()65})6667it('renders column selector button', () => {68const onColumnSelectionChange = jest.fn()69render(70<ColumnSelector71sessions={mockSessions}72selectedColumns={[]}73onColumnSelectionChange={onColumnSelectionChange}74/>75)7677const button = screen.getByRole('button', { name: /select columns/i })78expect(button).toBeInTheDocument()7980expect(screen.getByTestId('ViewColumnIcon')).toBeInTheDocument()81})8283it('opens dialog when button is clicked', async () => {84const onColumnSelectionChange = jest.fn()85render(86<ColumnSelector87sessions={mockSessions}88selectedColumns={[]}89onColumnSelectionChange={onColumnSelectionChange}90/>91)9293const user = userEvent.setup()94await user.click(screen.getByRole('button', { name: /select columns/i }))9596expect(screen.getByText('Select Columns to Display')).toBeInTheDocument()97expect(screen.getByText('Select capability fields to display as additional columns:')).toBeInTheDocument()98})99100it('displays available columns from session capabilities', async () => {101const onColumnSelectionChange = jest.fn()102render(103<ColumnSelector104sessions={mockSessions}105selectedColumns={[]}106onColumnSelectionChange={onColumnSelectionChange}107/>108)109110const user = userEvent.setup()111await user.click(screen.getByRole('button', { name: /select columns/i }))112113expect(screen.getByLabelText('browserName')).toBeInTheDocument()114expect(screen.getByLabelText('browserVersion')).toBeInTheDocument()115expect(screen.getByLabelText('platformName')).toBeInTheDocument()116expect(screen.getByLabelText('acceptInsecureCerts')).toBeInTheDocument()117expect(screen.getByLabelText('acceptSslCerts')).toBeInTheDocument()118})119120it('shows selected columns as checked', async () => {121const onColumnSelectionChange = jest.fn()122const selectedColumns = ['browserName', 'platformName']123124render(125<ColumnSelector126sessions={mockSessions}127selectedColumns={selectedColumns}128onColumnSelectionChange={onColumnSelectionChange}129/>130)131132const user = userEvent.setup()133await user.click(screen.getByRole('button', { name: /select columns/i }))134135expect(screen.getByLabelText('browserName')).toBeChecked()136expect(screen.getByLabelText('platformName')).toBeChecked()137138expect(screen.getByLabelText('browserVersion')).not.toBeChecked()139expect(screen.getByLabelText('acceptInsecureCerts')).not.toBeChecked()140})141142it('toggles column selection when checkbox is clicked', async () => {143const onColumnSelectionChange = jest.fn()144const selectedColumns = ['browserName']145146render(147<ColumnSelector148sessions={mockSessions}149selectedColumns={selectedColumns}150onColumnSelectionChange={onColumnSelectionChange}151/>152)153154const user = userEvent.setup()155await user.click(screen.getByRole('button', { name: /select columns/i }))156157await user.click(screen.getByLabelText('platformName'))158await user.click(screen.getByLabelText('browserName'))159160await user.click(screen.getByRole('button', { name: /apply/i }))161162expect(onColumnSelectionChange).toHaveBeenCalledWith(['platformName'])163})164165it('selects all columns when "Select All" is clicked', async () => {166const onColumnSelectionChange = jest.fn()167168render(169<ColumnSelector170sessions={mockSessions}171selectedColumns={[]}172onColumnSelectionChange={onColumnSelectionChange}173/>174)175176const user = userEvent.setup()177await user.click(screen.getByRole('button', { name: /select columns/i }))178179await user.click(screen.getByLabelText(/select all/i))180181await user.click(screen.getByRole('button', { name: /apply/i }))182183expect(onColumnSelectionChange).toHaveBeenCalled()184const allColumns = ['browserName', 'browserVersion', 'platformName', 'acceptInsecureCerts', 'acceptSslCerts']185expect(onColumnSelectionChange.mock.calls[0][0].sort()).toEqual(allColumns.sort())186})187188it('unselects all columns when "Select All" is unchecked', async () => {189const onColumnSelectionChange = jest.fn()190const allColumns = ['browserName', 'browserVersion', 'platformName', 'acceptInsecureCerts', 'acceptSslCerts']191192render(193<ColumnSelector194sessions={mockSessions}195selectedColumns={allColumns}196onColumnSelectionChange={onColumnSelectionChange}197/>198)199200const user = userEvent.setup()201await user.click(screen.getByRole('button', { name: /select columns/i }))202203await user.click(screen.getByLabelText(/select all/i))204205await user.click(screen.getByRole('button', { name: /apply/i }))206207expect(onColumnSelectionChange).toHaveBeenCalledWith([])208})209210it('closes dialog without changes when Cancel is clicked', async () => {211const onColumnSelectionChange = jest.fn()212const selectedColumns = ['browserName']213214render(215<ColumnSelector216sessions={mockSessions}217selectedColumns={selectedColumns}218onColumnSelectionChange={onColumnSelectionChange}219/>220)221222const user = userEvent.setup()223await user.click(screen.getByRole('button', { name: /select columns/i }))224225await user.click(screen.getByLabelText('platformName'))226227await user.click(screen.getByRole('button', { name: /cancel/i }))228229expect(onColumnSelectionChange).not.toHaveBeenCalled()230})231232it('saves capability keys to localStorage', async () => {233render(234<ColumnSelector235sessions={mockSessions}236selectedColumns={[]}237onColumnSelectionChange={jest.fn()}238/>239)240241expect(localStorageMock.setItem).toHaveBeenCalledWith(242'selenium-grid-all-capability-keys',243expect.any(String)244)245246const savedKeys = JSON.parse(localStorageMock.setItem.mock.calls[0][1])247expect(savedKeys).toContain('browserName')248expect(savedKeys).toContain('browserVersion')249expect(savedKeys).toContain('platformName')250expect(savedKeys).toContain('acceptInsecureCerts')251expect(savedKeys).toContain('acceptSslCerts')252})253254it('loads capability keys from localStorage', async () => {255const savedKeys = ['browserName', 'customCapability', 'platformName']256localStorageMock.getItem.mockReturnValueOnce(JSON.stringify(savedKeys))257258render(259<ColumnSelector260sessions={[]}261selectedColumns={[]}262onColumnSelectionChange={jest.fn()}263/>264)265266const user = userEvent.setup()267await user.click(screen.getByRole('button', { name: /select columns/i }))268269expect(screen.getByLabelText('browserName')).toBeInTheDocument()270expect(screen.getByLabelText('customCapability')).toBeInTheDocument()271expect(screen.getByLabelText('platformName')).toBeInTheDocument()272})273274275