Skip to content

Commit f3f8ae3

Browse files
committed
add tests
1 parent 2c92a0e commit f3f8ae3

File tree

1 file changed

+110
-35
lines changed

1 file changed

+110
-35
lines changed

packages/react-aria-components/test/Tree.test.tsx

Lines changed: 110 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,46 +1971,121 @@ describe('Tree', () => {
19711971
expect(checkbox1).toBePartiallyChecked();
19721972
expect(checkbox2).toBePartiallyChecked();
19731973
});
1974-
1975-
it('should work with selectionStrategy=parent', async () => {
1976-
let {getAllByRole, getByLabelText} = render(<DynamicTree treeProps={{selectionMode: 'multiple', selectionPropagation: true, selectionStrategy: 'parent'}} />);
1977-
let row = getByLabelText('Projects');
1978-
let checkbox = within(row).getByRole('checkbox');
1979-
1980-
await user.click(checkbox);
1981-
expect(onSelectionChange).toHaveBeenCalledTimes(1);
1982-
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(['projects']));
1983-
1984-
let expectedKeys = collectIds(items, 'projects');
1985-
for (let row of getAllByRole('row')) {
1974+
1975+
describe('should work with selectionStrategy=parent', () => {
1976+
it('should select all children when the parent is selected', async () => {
1977+
let {getAllByRole, getByLabelText} = render(
1978+
<DynamicTree
1979+
treeProps={{
1980+
selectionMode: 'multiple',
1981+
selectionPropagation: true,
1982+
selectionStrategy: 'parent'
1983+
}} />
1984+
);
1985+
1986+
let row = getByLabelText('Projects');
19861987
let checkbox = within(row).getByRole('checkbox');
1987-
if (expectedKeys.has(row.dataset.key)) {
1988-
expect(checkbox).toBeChecked();
1989-
} else {
1990-
expect(checkbox).not.toBeChecked();
1988+
await user.click(checkbox);
1989+
1990+
let expectedKeys = collectIds(items, 'projects');
1991+
for (let row of getAllByRole('row')) {
1992+
let checkbox = within(row).getByRole('checkbox');
1993+
if (expectedKeys.has(row.dataset.key)) {
1994+
expect(checkbox).toBeChecked();
1995+
} else {
1996+
expect(checkbox).not.toBeChecked();
1997+
}
19911998
}
1992-
}
1999+
2000+
expect(onSelectionChange).toHaveBeenCalledTimes(1);
2001+
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(['projects']));
2002+
});
2003+
2004+
it('should select the parent when all children are selected', async () => {
2005+
let {getAllByRole, getByLabelText} = render(
2006+
<DynamicTree
2007+
treeProps={{
2008+
selectionMode: 'multiple',
2009+
selectionPropagation: true,
2010+
selectionStrategy: 'parent'
2011+
}} />
2012+
);
2013+
2014+
let row = getByLabelText('Reports 1ABC');
2015+
let checkbox = within(row).getByRole('checkbox');
2016+
await user.click(checkbox);
2017+
2018+
let expectedKeys = collectIds(items, 'reports-1A');
2019+
for (let row of getAllByRole('row')) {
2020+
let checkbox = within(row).getByRole('checkbox');
2021+
if (expectedKeys.has(row.dataset.key)) {
2022+
expect(checkbox).toBeChecked();
2023+
} else {
2024+
expect(checkbox).not.toBeChecked();
2025+
}
2026+
}
2027+
2028+
expect(onSelectionChange).toHaveBeenCalledTimes(1);
2029+
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(['reports-1A']));
2030+
});
19932031
});
1994-
1995-
it('should work with selectionStrategy=child', async () => {
1996-
let {getAllByRole, getByLabelText} = render(<DynamicTree treeProps={{selectionMode: 'multiple', selectionPropagation: true, selectionStrategy: 'child'}} />);
1997-
let row = getByLabelText('Project 2');
1998-
let checkbox = within(row).getByRole('checkbox');
1999-
2000-
await user.click(checkbox);
2001-
2002-
let expectedKeys = collectIds(items, 'project-2');
2003-
for (let row of getAllByRole('row')) {
2032+
2033+
describe('should work with selectionStrategy=child', () => {
2034+
it('should select all children when the parent is selected', async () => {
2035+
let {getAllByRole, getByLabelText} = render(
2036+
<DynamicTree
2037+
treeProps={{
2038+
selectionMode: 'multiple',
2039+
selectionPropagation: true,
2040+
selectionStrategy: 'child'
2041+
}} />
2042+
);
2043+
2044+
let row = getByLabelText('Project 2');
20042045
let checkbox = within(row).getByRole('checkbox');
2005-
if (expectedKeys.has(row.dataset.key)) {
2006-
expect(checkbox).toBeChecked();
2007-
} else {
2008-
expect(checkbox).not.toBeChecked();
2046+
await user.click(checkbox);
2047+
2048+
let expectedKeys = collectIds(items, 'project-2');
2049+
for (let row of getAllByRole('row')) {
2050+
let checkbox = within(row).getByRole('checkbox');
2051+
if (expectedKeys.has(row.dataset.key)) {
2052+
expect(checkbox).toBeChecked();
2053+
} else {
2054+
expect(checkbox).not.toBeChecked();
2055+
}
20092056
}
2010-
}
2011-
2012-
expect(onSelectionChange).toHaveBeenCalledTimes(1);
2013-
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(['project-2A', 'project-2B', 'project-2C']));
2057+
2058+
expect(onSelectionChange).toHaveBeenCalledTimes(1);
2059+
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(['project-2A', 'project-2B', 'project-2C']));
2060+
});
2061+
2062+
it('should select the parent when all children are selected', async () => {
2063+
let {getAllByRole, getByLabelText} = render(
2064+
<DynamicTree
2065+
treeProps={{
2066+
selectionMode: 'multiple',
2067+
selectionPropagation: true,
2068+
selectionStrategy: 'child'
2069+
}} />
2070+
);
2071+
2072+
let row = getByLabelText('Reports 1ABC');
2073+
let checkbox = within(row).getByRole('checkbox');
2074+
await user.click(checkbox);
2075+
2076+
let expectedKeys = collectIds(items, 'reports-1A');
2077+
for (let row of getAllByRole('row')) {
2078+
let checkbox = within(row).getByRole('checkbox');
2079+
if (expectedKeys.has(row.dataset.key)) {
2080+
expect(checkbox).toBeChecked();
2081+
} else {
2082+
expect(checkbox).not.toBeChecked();
2083+
}
2084+
}
2085+
2086+
expect(onSelectionChange).toHaveBeenCalledTimes(1);
2087+
expect(new Set(onSelectionChange.mock.calls[0][0])).toEqual(new Set(['reports-1ABC']));
2088+
});
20142089
});
20152090
});
20162091
});

0 commit comments

Comments
 (0)