Add unit tests for AdminAutoHeightTextInput

pull/6931/head
Karl Hobley 2021-01-18 17:07:37 +00:00 zatwierdzone przez Matt Westcott
rodzic e23e7c1c6e
commit 0a2f71856a
2 zmienionych plików z 50 dodań i 1 usunięć

Wyświetl plik

@ -92,7 +92,7 @@ window.telepath.register('wagtail.widgets.PageChooser', PageChooser);
class AdminAutoHeightTextInput extends Widget {
render(placeholder, name, id, initialState) {
const boundWidget = super.render(placeholder, name, id, initialState);
window.autosize($(id));
window.autosize($('#' + id));
return boundWidget;
}
}

Wyświetl plik

@ -197,3 +197,52 @@ describe('telepath: wagtail.widgets.PageChooser', () => {
expect(document.activeElement).toBe(document.querySelector('.unchosen button'));
});
});
describe('telepath: wagtail.widgets.AdminAutoHeightTextInput', () => {
let boundWidget;
beforeEach(() => {
window.autosize = jest.fn();
// Create a placeholder to render the widget
document.body.innerHTML = '<div id="placeholder"></div>';
// Unpack and render a textarea using the AdminAutoHeightTextInput widget
const widgetDef = window.telepath.unpack({
_type: 'wagtail.widgets.AdminAutoHeightTextInput',
_args: [
'<textarea name="__NAME__" cols="40" rows="1" id="__ID__"></textarea>',
'__ID__'
]
});
boundWidget = widgetDef.render($('#placeholder'), 'the-name', 'the-id', 'The Value');
});
test('it renders correctly', () => {
expect(document.body.innerHTML).toBe('<textarea name="the-name" cols="40" rows="1" id="the-id"></textarea>');
expect(document.querySelector('textarea').value).toBe('The Value');
});
test('window.autosize was called', () => {
expect(window.autosize.mock.calls.length).toBe(1);
expect(window.autosize.mock.calls[0][0].get(0)).toBe(document.querySelector('textarea'));
});
test('getValue() returns the current value', () => {
expect(boundWidget.getValue()).toBe('The Value');
});
test('getState() returns the current state', () => {
expect(boundWidget.getState()).toBe('The Value');
});
test('setState() changes the current state', () => {
boundWidget.setState('The new Value');
expect(document.querySelector('textarea').value).toBe('The new Value');
});
test('focus() focuses the text input', () => {
boundWidget.focus();
expect(document.activeElement).toBe(document.querySelector('textarea'));
});
});