@testing-library/dom#getByText JavaScript Examples
The following examples show how to use
@testing-library/dom#getByText.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: unit.spec.js From js-test-basic with MIT License | 6 votes |
it('생성시 counter의 상태에 버튼과 초기값을 렌더링한다.', () => {
createCounter.mockImplementation(() => ({
val: () => 10,
isMin: () => false,
isMax: () => false
}));
createUICounter(container);
expect(getByText(container, '+')).toBeVisible();
expect(getByText(container, '-')).toBeVisible();
expect(getByText(container, '10')).toBeVisible();
});
Example #2
Source File: unit.spec.js From js-test-basic with MIT License | 6 votes |
it('isMin/isMax 값이 true이면 -/+ 버튼은 disabled 상태가 된다.', () => {
createCounter.mockImplementation(() => ({
val: () => 10,
isMin: () => true,
isMax: () => true
}));
createUICounter(container);
expect(getByText(container, '-')).toBeDisabled();
expect(getByText(container, '+')).toBeDisabled();
});
Example #3
Source File: unit.spec.js From js-test-basic with MIT License | 6 votes |
it('+ 버튼 클릭시 counter의 inc()를 호출한 후 다시 렌더링한다.', () => {
let value = 10;
const counterInc = jest.fn().mockImplementation(() => {
value = 11;
});
createCounter.mockImplementation(() => ({
val: () => value,
isMin: () => false,
isMax: () => false,
inc: counterInc
}));
createUICounter(container);
fireEvent.click(getByText(container, '+'));
expect(counterInc).toHaveBeenCalled();
expect(getByText(container, '11')).toBeVisible();
});
Example #4
Source File: unit.spec.js From js-test-basic with MIT License | 6 votes |
it('- 버튼 클릭시 counter의 dec()를 호출한 후 다시 렌더링한다.', () => {
let value = 10;
const counterDec = jest.fn().mockImplementation(() => {
value = 9;
});
createCounter.mockImplementation(() => ({
val: () => value,
isMin: () => false,
isMax: () => false,
dec: counterDec
}));
createUICounter(container);
fireEvent.click(getByText(container, '-'));
expect(counterDec).toHaveBeenCalled();
expect(getByText(container, '9')).toBeVisible();
});
Example #5
Source File: LibraryAccessForm.spec.jsx From frontend-app-library-authoring with GNU Affero General Public License v3.0 | 6 votes |
testSuite('<LibraryAccessForm />', () => {
it('Renders an error for the email field', async () => {
const library = libraryFactory();
const props = { library, errorFields: { email: 'Too difficult to remember.' }, ...commonMocks() };
const { container } = await ctxRender(<LibraryAccessFormContainer {...props} />);
expect(getByText(container, /Too difficult/)).toBeTruthy();
});
it('Submits and adds a new user.', async () => {
const library = libraryFactory();
const props = { library, ...commonMocks() };
const { addUser } = props;
const user = userFactory();
addUser.mockImplementation(() => immediate(user));
const { container } = await ctxRender(<LibraryAccessFormContainer {...props} />);
const emailField = getByLabelText(container, 'Email');
fireEvent.change(emailField, { target: { value: '[email protected]' } });
const submitButton = getByRole(container, 'button', { name: /Submit/ });
fireEvent.click(submitButton);
await waitFor(() => expect(addUser).toHaveBeenCalledWith({
libraryId: library.id, data: { email: '[email protected]', access_level: LIBRARY_ACCESS.READ },
}));
expect(emailField.value).toBe('');
});
it('Closes out', async () => {
const library = libraryFactory();
const props = { library, ...commonMocks() };
const { setShowAdd } = props;
const { container } = await ctxRender(
<LibraryAccessFormContainer
{...props}
/>,
);
const button = getByRole(container, 'button', { name: /Cancel/ });
fireEvent.click(button);
expect(setShowAdd).toHaveBeenCalledWith(false);
});
});
Example #6
Source File: UserAccessContainer.spec.jsx From frontend-app-library-authoring with GNU Affero General Public License v3.0 | 4 votes |
testSuite('<UserAccessWidgetContainer />', () => {
[
[LIBRARY_ACCESS.READ, 'Read Only'],
[LIBRARY_ACCESS.ADMIN, 'Admin'],
[LIBRARY_ACCESS.AUTHOR, 'Author'],
].forEach(([accessLevel, text]) => {
it(`Should render a badge labeled ${text} when the target user's access level is ${accessLevel}.`, async () => {
const [library] = libraryFactoryLine();
// userId doesn't exist on users pulled from the team listings, but it does exist on authenticated users.
const [user, currentUser] = userFactoryLine([{ access_level: accessLevel }, { userId: 3 }]);
const props = {
library, user, multipleAdmins: false, ...commonMocks(),
};
await ctxRender(
<UserAccessWidgetContainer
{...props}
/>,
{ context: { authenticatedUser: currentUser } },
);
const badge = screen.getByText(text);
expect(badge).toBeTruthy();
});
});
[
[true, LIBRARY_ACCESS.ADMIN, false, false],
[true, LIBRARY_ACCESS.ADMIN, true, true],
[true, LIBRARY_ACCESS.AUTHOR, true, false],
[true, LIBRARY_ACCESS.AUTHOR, false, false],
[true, LIBRARY_ACCESS.READ, true, false],
[true, LIBRARY_ACCESS.READ, false, false],
[false, LIBRARY_ACCESS.ADMIN, true, false],
[false, LIBRARY_ACCESS.ADMIN, false, false],
].forEach(async ([isAdmin, accessLevel, multipleAdmins, buttonsShown]) => {
let testName = 'The admin privilege management buttons are ';
if (!buttonsShown) {
testName += 'not ';
}
testName += 'shown when the viewer is ';
if (!isAdmin) {
testName += 'not ';
}
testName += `an admin and the subject's access level is ${accessLevel} and there are `;
if (!multipleAdmins) {
testName += 'multiple admins.';
}
it(testName, async () => {
const [library] = libraryFactoryLine();
const [currentUser, targetUser] = userFactoryLine([{}, { access_level: accessLevel }]);
const props = {
library, user: targetUser, multipleAdmins, isAdmin, ...commonMocks(),
};
const { container } = await ctxRender(
<UserAccessWidgetContainer
{...props}
/>,
{ context: { authenticatedUser: currentUser } },
);
if (buttonsShown) {
const removeButton = getByRole(container, 'button', { name: /Remove Admin/ });
expect(removeButton).toBeTruthy();
} else {
const buttonList = queryAllByRole(container, 'button', { name: /Remove Admin/ });
expect(buttonList.length).toBe(0);
if (isAdmin && targetUser.access_level === LIBRARY_ACCESS.ADMIN) {
expect(getByText(container, /Promote another member/)).toBeTruthy();
} else {
expect(queryAllByText(container, /Promote another member/).length).toBe(0);
}
}
});
});
[
[true, LIBRARY_ACCESS.ADMIN, false],
[true, LIBRARY_ACCESS.AUTHOR, true],
[true, LIBRARY_ACCESS.READ, false],
[false, LIBRARY_ACCESS.AUTHOR, false],
].forEach(async ([isAdmin, accessLevel, buttonsShown]) => {
let testName = 'The staff management buttons are ';
if (!buttonsShown) {
testName += 'not ';
}
testName += 'visible when the viewer is ';
if (!isAdmin) {
testName += 'not ';
}
testName += `an admin and the subject's current access_level is ${accessLevel}.`;
it(testName, async () => {
const [library] = libraryFactoryLine();
const [currentUser, targetUser] = userFactoryLine([{}, { access_level: accessLevel }]);
const props = {
library, user: targetUser, multipleAdmins: false, isAdmin, ...commonMocks(),
};
const { container } = await ctxRender(
<UserAccessWidgetContainer
{...props}
/>,
{ context: { authenticatedUser: currentUser } },
);
if (buttonsShown) {
const removeButton = getByRole(container, 'button', { name: /Remove Author/ });
expect(removeButton).toBeTruthy();
const addButton = getByRole(container, 'button', { name: /Add Admin/ });
expect(addButton).toBeTruthy();
} else {
const buttonList = queryAllByRole(container, 'button', { name: /(Remove Author|Add Admin)/ });
expect(buttonList.length).toBe(0);
}
});
});
[
[true, LIBRARY_ACCESS.ADMIN, false],
[true, LIBRARY_ACCESS.AUTHOR, false],
[true, LIBRARY_ACCESS.READ, true],
[false, LIBRARY_ACCESS.READ, false],
].forEach(async ([isAdmin, accessLevel, buttonShown]) => {
let testName = 'The add author privileges button is ';
if (!buttonShown) {
testName += 'not ';
}
testName += 'visible if the viewer is ';
if (!isAdmin) {
testName += 'not ';
}
testName += `an admin and the subject's access level is ${accessLevel}.`;
it(testName, async () => {
const [library] = libraryFactoryLine();
const [currentUser, targetUser] = userFactoryLine([{}, { access_level: accessLevel }]);
const props = {
library, user: targetUser, multipleAdmins: false, isAdmin, ...commonMocks(),
};
const { container } = await ctxRender(
<UserAccessWidgetContainer
{...props}
/>,
{ context: { authenticatedUser: currentUser } },
);
const buttonList = queryAllByRole(container, 'button', { name: /Add Author/ });
if (buttonShown) {
expect(buttonList.length).toBe(1);
} else {
expect(buttonList.length).toBe(0);
}
});
});
[
[true, LIBRARY_ACCESS.ADMIN, false, false],
[true, LIBRARY_ACCESS.ADMIN, true, true],
[true, LIBRARY_ACCESS.AUTHOR, true, true],
[true, LIBRARY_ACCESS.AUTHOR, false, true],
[true, LIBRARY_ACCESS.READ, true, true],
[true, LIBRARY_ACCESS.READ, false, true],
[false, LIBRARY_ACCESS.ADMIN, true, false],
[false, LIBRARY_ACCESS.ADMIN, false, false],
[false, LIBRARY_ACCESS.AUTHOR, true, false],
[false, LIBRARY_ACCESS.AUTHOR, false, false],
[false, LIBRARY_ACCESS.READ, true, false],
[false, LIBRARY_ACCESS.READ, false, false],
].forEach(([isAdmin, accessLevel, multipleAdmins, buttonShown]) => {
let testName = 'The button for removing a user is ';
if (buttonShown) {
testName += 'present';
} else {
testName += 'not present';
}
testName += ' when the viewer is ';
if (!isAdmin) {
testName += 'not ';
}
testName += 'an admin and there are ';
if (!multipleAdmins) {
testName += ' not ';
}
testName += 'multiple admins in a library.';
it(testName, async () => {
const [library] = libraryFactoryLine();
const [currentUser, targetUser] = userFactoryLine([{}, { access_level: accessLevel }]);
const props = {
library, user: targetUser, multipleAdmins, isAdmin, ...commonMocks(),
};
const { container } = await ctxRender(
<UserAccessWidgetContainer
{...props}
/>,
{ context: { authenticatedUser: currentUser } },
);
const buttonList = queryAllByRole(container, 'button', { name: /Remove user/ });
if (buttonShown) {
expect(buttonList.length).toBe(1);
} else {
expect(buttonList.length).toBe(0);
}
});
});
});