decentraland-ui
Version:
Decentraland's UI components and styles
83 lines (82 loc) • 3.46 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var react_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
var RadioOptions_1 = require("./RadioOptions");
function renderRadioOptions(props) {
if (props === void 0) { props = {}; }
return (0, react_2.render)(react_1.default.createElement(RadioOptions_1.RadioOptions, __assign({ value: undefined, options: [], onChange: jest.fn() }, props)));
}
var renderedComponent;
var options;
describe('when rendering the component', function () {
beforeEach(function () {
options = [
{ name: 'First option', value: 'first_option' },
{ name: 'Second option', value: 'second_option' },
{ name: 'Third option', value: 'third_option', info: 'Some info!' }
];
renderedComponent = renderRadioOptions({ options: options });
});
it('should render a radio button for each option', function () {
var getByText = renderedComponent.getByText;
options.forEach(function (option) {
var radio = getByText(option.name);
expect(radio).toBeInTheDocument();
if (option.info) {
expect(radio.querySelector('.dui-info-tooltip__trigger')).toBeInTheDocument();
}
});
});
});
describe('when clicking on an option', function () {
var value;
var onChange;
beforeEach(function () {
onChange = jest.fn();
options = [
{ name: 'First option', value: 'first_option' },
{ name: 'Second option', value: 'second_option' },
{ name: 'Third option', value: 'third_option', info: 'Some info!' }
];
});
describe('and the option is already selected', function () {
beforeEach(function () {
value = options[0].value;
renderedComponent = renderRadioOptions({ options: options, value: value, onChange: onChange });
});
it('should not call the onChange callback', function () {
var getByText = renderedComponent.getByText;
var radio = getByText(options[0].name);
user_event_1.default.click(radio);
expect(onChange).not.toHaveBeenCalledWith(value);
});
});
describe('and the option is not selected', function () {
beforeEach(function () {
value = options[1].value;
renderedComponent = renderRadioOptions({ options: options, value: value, onChange: onChange });
});
it('should call the onChange callback with the new value', function () {
var getByText = renderedComponent.getByText;
var radio = getByText(options[0].name);
user_event_1.default.click(radio);
expect(onChange).toHaveBeenCalledWith(options[0].value);
});
});
});