mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-02 14:59:53 +08:00
* Add `eslint-plugin-{jest-dom,testing-library}`
* Enable `eslint-plugin-{jest-dom,testing-library}` rules
* Run `eslint --fix`
* refactor: apply eslint hints in FormPassword.test.tsx
* refactor: fix test in Table.test.tsx
* refactor: apply eslint hints in Table.test.tsx
* refactor: apply eslint hints in multiplefiles
* refactor: apply eslint hints in multiple files
* refactor: allow direct node access
* test: fix
* refactor: ButtonSso FileldInput
* refactor: FieldSelect
* refactor: FieldSelect
* refactor: MenuAction
* refactor: ShareFeedback
* refactor: FormLogin
* refactor: LabelSelector
* refactor: Welcome
* refactor: useUrlFiltering
* refactor: Login
* refactor: LoginTrait
* refactor: apply eslint hints in multiple files
* refactor: apply eslint hints in multiple files
* refactor: apply eslint hints in MenageDevices
* refactor: Indicator and FieldSelect tests
* refactor: Portal tests
* fix: use @testing-library/react instead of react-test-renderer
* refactor: use find without assertion
* fix: add webapps.e
* refactor: find by instead of get by+wait for
* fix: remove unused waitFor
* test: LoginTrait checkboxes count
* test: SlideTabs check tabs len
Co-authored-by: Ryan Clark <ryan@ryanclark.me>
125 lines
3.3 KiB
TypeScript
125 lines
3.3 KiB
TypeScript
/**
|
|
* Copyright 2020 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 { render, fireEvent, screen } from 'design/utils/testing';
|
|
|
|
import Logger from '../../libs/logger';
|
|
|
|
import Validator, { Validation, useValidation } from './Validation';
|
|
|
|
jest.mock('../../libs/logger', () => {
|
|
const mockLogger = {
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
};
|
|
|
|
return {
|
|
create: () => mockLogger,
|
|
};
|
|
});
|
|
|
|
test('methods of Validator: sub, unsub, validate', () => {
|
|
const mockCb1 = jest.fn();
|
|
const mockCb2 = jest.fn();
|
|
const validator = new Validator();
|
|
|
|
// test suscribe
|
|
validator.subscribe(mockCb1);
|
|
validator.subscribe(mockCb2);
|
|
|
|
// test validate runs all subscribed cb's
|
|
expect(validator.validate()).toBe(true);
|
|
expect(mockCb1).toHaveBeenCalledTimes(1);
|
|
expect(mockCb2).toHaveBeenCalledTimes(1);
|
|
jest.clearAllMocks();
|
|
|
|
// test unsubscribe method removes correct cb
|
|
validator.unsubscribe(mockCb2);
|
|
expect(validator.validate()).toBe(true);
|
|
expect(mockCb1).toHaveBeenCalledTimes(1);
|
|
expect(mockCb2).toHaveBeenCalledTimes(0);
|
|
});
|
|
|
|
test('methods of Validator: addResult, reset', () => {
|
|
const validator = new Validator();
|
|
|
|
// test addResult for nil object
|
|
const result = null;
|
|
validator.addResult(result);
|
|
expect(Logger.create().error).toHaveBeenCalledTimes(1);
|
|
|
|
// test addResult for boolean
|
|
validator.addResult(true);
|
|
expect(validator.valid).toBe(false);
|
|
|
|
// test addResult with incorrect object
|
|
let resultObj = {};
|
|
validator.addResult(resultObj);
|
|
expect(validator.valid).toBe(false);
|
|
|
|
// test addResult with correct object with "valid" prop from prior test set to false
|
|
resultObj = { valid: true };
|
|
validator.addResult(resultObj);
|
|
expect(validator.valid).toBe(false);
|
|
|
|
// test reset
|
|
validator.reset();
|
|
expect(validator.valid).toBe(true);
|
|
expect(validator.validating).toBe(false);
|
|
|
|
// test addResult with correct object with "valid" prop reset to true
|
|
validator.addResult(resultObj);
|
|
expect(validator.valid).toBe(true);
|
|
});
|
|
|
|
test('trigger validation via useValidation hook', () => {
|
|
let validator = null;
|
|
const Button = () => {
|
|
validator = useValidation();
|
|
return <button role="button" onClick={() => validator.validate()} />;
|
|
};
|
|
|
|
render(
|
|
<Validation>
|
|
<Button />
|
|
</Validation>
|
|
);
|
|
fireEvent.click(screen.getByRole('button'));
|
|
expect(validator.validating).toBe(true);
|
|
});
|
|
|
|
test('trigger validation via render function', () => {
|
|
let validator = null;
|
|
|
|
render(
|
|
<Validation>
|
|
{props => (
|
|
<button
|
|
role="button"
|
|
onClick={() => {
|
|
validator = props.validator;
|
|
validator.validate();
|
|
}}
|
|
/>
|
|
)}
|
|
</Validation>
|
|
);
|
|
fireEvent.click(screen.getByRole('button'));
|
|
expect(validator.validating).toBe(true);
|
|
});
|