Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v13] Web: Fix not being able to search for locks in table #31581

Merged
merged 1 commit into from Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions web/packages/teleport/src/LocksV2/Locks/Locks.test.tsx
@@ -0,0 +1,81 @@
/**
* Copyright 2023 Gravitational, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';
import { MemoryRouter } from 'react-router';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { render, fireEvent, screen } from 'design/utils/testing';

import { ContextProvider } from 'teleport';
import { createTeleportContext } from 'teleport/mocks/contexts';
import cfg from 'teleport/config';

import { Locks } from './Locks';

test('lock search', async () => {
const server = setupServer(
rest.get(cfg.getLocksUrl(), (req, res, ctx) => {
return res(
ctx.json([
{
name: 'lock-name-1',
targets: {
user: 'lock-user',
},
},
{
name: 'lock-name-2',
targets: {
role: 'lock-role-1',
},
},
{
name: 'lock-name-3',
targets: {
role: 'lock-role-2',
},
},
])
);
})
);

server.listen();

const ctx = createTeleportContext();

render(
<MemoryRouter>
<ContextProvider ctx={ctx}>
<Locks />
</ContextProvider>
</MemoryRouter>
);

const rows = await screen.findAllByText(/lock-/i);
expect(rows).toHaveLength(3);

// Test searching.
fireEvent.change(screen.getByPlaceholderText(/search/i), {
target: { value: 'lock-role' },
});

expect(screen.queryAllByText(/lock-role/i)).toHaveLength(2);
expect(screen.queryByText(/lock-user/i)).not.toBeInTheDocument();

server.close();
});
10 changes: 5 additions & 5 deletions web/packages/teleport/src/LocksV2/Locks/Locks.tsx
Expand Up @@ -177,17 +177,17 @@ function getFormattedDate(d: string): string {
}
}

function lockTargetsMatcher(
export function lockTargetsMatcher(
targetValue: any,
searchValue: string,
propName: keyof Lock & string
) {
if (propName === 'targets') {
return targetValue.some(
({ name, value }) =>
return (targetValue as LockTarget[]).some(
({ name, kind }) =>
name.toLocaleUpperCase().includes(searchValue) ||
value.toLocaleUpperCase().includes(searchValue) ||
`${name}: ${value}`.toLocaleUpperCase().includes(searchValue)
kind.toLocaleUpperCase().includes(searchValue) ||
`${kind}: ${name}`.toLocaleUpperCase().includes(searchValue)
);
}
}
Expand Down