Skip to content

Commit 341c341

Browse files
committed
add tests
1 parent 3208eb0 commit 341c341

File tree

7 files changed

+193
-11
lines changed

7 files changed

+193
-11
lines changed

packages/compass-global-writes/src/components/index.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('Compass GlobalWrites Plugin', function () {
1919
await renderWithStore(
2020
<GlobalWrites shardingStatus={'SUBMITTING_FOR_SHARDING'} />
2121
);
22-
expect(screen.getByTestId('shard-collection-button')).to.exist;
22+
expect(screen.getByTestId('unmanage-collection-button')).to.exist;
2323
});
2424

2525
it('renders plugin in SHARDING state', async function () {

packages/compass-global-writes/src/components/shard-key-markup.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ export function ShardKeyMarkup({
2323
}: ShardKeyMarkupProps) {
2424
let markup = shardKey.fields
2525
.map(
26-
(field) => `"${field.name}"` + (showMetaData ? ` (${field.type}) ` : '')
26+
(field) =>
27+
`"${field.name}"` +
28+
(showMetaData ? ` (${field.type.toLowerCase()})` : '')
2729
)
2830
.join(', ');
2931
if (showMetaData) {
3032
markup += ` - unique: ${String(shardKey.isUnique)}`;
3133
}
3234
return (
3335
<div className={codeBlockContainerStyles}>
34-
<Body data-testid="shardkey-description-title">
36+
<Body data-testid={`${type}-shardkey-description-title`}>
3537
{type === 'existing' ? (
3638
<>
3739
<strong>{namespace}</strong> is configured with the following shard
@@ -41,7 +43,7 @@ export function ShardKeyMarkup({
4143
<>You requested to use the shard key:</>
4244
)}
4345
</Body>
44-
<Code language="js" data-testid="shardkey-description-content">
46+
<Code language="js" data-testid={`${type}-shardkey-description-content`}>
4547
{markup}
4648
</Code>
4749
</div>

packages/compass-global-writes/src/components/states/shard-key-correct.spec.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe('Compass GlobalWrites Plugin', function () {
2828
namespace: 'db1.coll1',
2929
shardKey: {
3030
fields: [
31-
{ type: 'HASHED', name: 'location' },
32-
{ type: 'RANGE', name: 'secondary' },
31+
{ type: 'RANGE', name: 'location' },
32+
{ type: 'HASHED', name: 'secondary' },
3333
],
3434
isUnique: false,
3535
},
@@ -66,7 +66,7 @@ describe('Compass GlobalWrites Plugin', function () {
6666
await renderWithProps({ onUnmanageNamespace, isUnmanagingNamespace: true });
6767

6868
const btn = await screen.findByTestId<HTMLButtonElement>(
69-
'shard-collection-button'
69+
'unmanage-collection-button'
7070
);
7171
expect(btn).to.be.visible;
7272
expect(btn.getAttribute('aria-disabled')).to.equal('true');
@@ -103,12 +103,16 @@ describe('Compass GlobalWrites Plugin', function () {
103103
it('Describes the shardKey', async function () {
104104
await renderWithProps();
105105

106-
const title = await screen.findByTestId('shardkey-description-title');
106+
const title = await screen.findByTestId(
107+
'existing-shardkey-description-title'
108+
);
107109
expect(title).to.be.visible;
108110
expect(title.textContent).to.equal(
109111
`${baseProps.namespace} is configured with the following shard key:`
110112
);
111-
const list = await screen.findByTestId('shardkey-description-content');
113+
const list = await screen.findByTestId(
114+
'existing-shardkey-description-content'
115+
);
112116
expect(list).to.be.visible;
113117
expect(list.textContent).to.contain(`"location", "secondary"`);
114118
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { expect } from 'chai';
3+
import { screen } from '@mongodb-js/testing-library-compass';
4+
import {
5+
ShardKeyInvalid,
6+
type ShardKeyInvalidProps,
7+
} from './shard-key-invalid';
8+
import { renderWithStore } from '../../../tests/create-store';
9+
10+
describe('Compass GlobalWrites Plugin', function () {
11+
const baseProps: ShardKeyInvalidProps = {
12+
namespace: 'db1.coll1',
13+
shardKey: {
14+
fields: [
15+
{ type: 'HASHED', name: 'not-location' },
16+
{ type: 'RANGE', name: 'secondary' },
17+
],
18+
isUnique: false,
19+
},
20+
};
21+
22+
function renderWithProps(
23+
props?: Partial<ShardKeyInvalidProps>,
24+
options?: Parameters<typeof renderWithStore>[1]
25+
) {
26+
return renderWithStore(
27+
<ShardKeyInvalid {...baseProps} {...props} />,
28+
options
29+
);
30+
}
31+
32+
it('Describes next steps', async function () {
33+
await renderWithProps();
34+
35+
expect(screen.findByText(/Please migrate the data in this collection/)).to
36+
.exist;
37+
});
38+
39+
it('Describes the shardKey (with metadata)', async function () {
40+
await renderWithProps();
41+
42+
const title = await screen.findByTestId(
43+
'existing-shardkey-description-title'
44+
);
45+
expect(title).to.be.visible;
46+
expect(title.textContent).to.equal(
47+
`${baseProps.namespace} is configured with the following shard key:`
48+
);
49+
const list = await screen.findByTestId(
50+
'existing-shardkey-description-content'
51+
);
52+
expect(list).to.be.visible;
53+
expect(list.textContent).to.contain(
54+
`"not-location" (hashed), "secondary" (range)`
55+
);
56+
expect(list.textContent).to.contain(`unique: false`);
57+
});
58+
});

packages/compass-global-writes/src/components/states/shard-key-invalid.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const paragraphStyles = css({
2222
gap: spacing[100],
2323
});
2424

25-
interface ShardKeyInvalidProps {
25+
export interface ShardKeyInvalidProps {
2626
shardKey?: ShardKey;
2727
namespace: string;
2828
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import React from 'react';
2+
import { expect } from 'chai';
3+
import { screen, userEvent } from '@mongodb-js/testing-library-compass';
4+
import {
5+
ShardKeyMismatch,
6+
type ShardKeyMismatchProps,
7+
} from './shard-key-mismatch';
8+
import { renderWithStore } from '../../../tests/create-store';
9+
import Sinon from 'sinon';
10+
11+
describe('Compass GlobalWrites Plugin', function () {
12+
const baseProps: ShardKeyMismatchProps = {
13+
namespace: 'db1.coll1',
14+
shardKey: {
15+
fields: [
16+
{ type: 'RANGE', name: 'location' },
17+
{ type: 'RANGE', name: 'secondary' },
18+
],
19+
isUnique: false,
20+
},
21+
requestedShardKey: {
22+
fields: [
23+
{ type: 'RANGE', name: 'location' },
24+
{ type: 'RANGE', name: 'tertiary' },
25+
],
26+
isUnique: true,
27+
},
28+
isUnmanagingNamespace: false,
29+
onUnmanageNamespace: () => {},
30+
};
31+
32+
function renderWithProps(
33+
props?: Partial<ShardKeyMismatchProps>,
34+
options?: Parameters<typeof renderWithStore>[1]
35+
) {
36+
return renderWithStore(
37+
<ShardKeyMismatch {...baseProps} {...props} />,
38+
options
39+
);
40+
}
41+
42+
it('Describes next steps', async function () {
43+
await renderWithProps();
44+
45+
expect(
46+
screen.findByText(
47+
/Please click the button below to unmanage this collection/
48+
)
49+
).to.exist;
50+
});
51+
52+
it('Describes the shardKey (with metadata)', async function () {
53+
await renderWithProps();
54+
55+
const title = await screen.findByTestId(
56+
'existing-shardkey-description-title'
57+
);
58+
expect(title).to.be.visible;
59+
expect(title.textContent).to.equal(
60+
`${baseProps.namespace} is configured with the following shard key:`
61+
);
62+
const list = await screen.findByTestId(
63+
'existing-shardkey-description-content'
64+
);
65+
expect(list).to.be.visible;
66+
expect(list.textContent).to.contain(
67+
`"location" (range), "secondary" (range)`
68+
);
69+
expect(list.textContent).to.contain(`unique: false`);
70+
});
71+
72+
it('Describes the requested shardKey (with metadata)', async function () {
73+
await renderWithProps();
74+
75+
const title = await screen.findByTestId(
76+
'requested-shardkey-description-title'
77+
);
78+
expect(title).to.be.visible;
79+
expect(title.textContent).to.equal('You requested to use the shard key:');
80+
const list = await screen.findByTestId(
81+
'requested-shardkey-description-content'
82+
);
83+
expect(list).to.be.visible;
84+
expect(list.textContent).to.contain(
85+
`"location" (range), "tertiary" (range)`
86+
);
87+
expect(list.textContent).to.contain(`unique: true`);
88+
});
89+
90+
it('Provides button to unmanage', async function () {
91+
const onUnmanageNamespace = Sinon.spy();
92+
await renderWithProps({ onUnmanageNamespace });
93+
94+
const btn = await screen.findByRole<HTMLButtonElement>('button', {
95+
name: /Unmanage collection/,
96+
});
97+
expect(btn).to.be.visible;
98+
99+
userEvent.click(btn);
100+
101+
expect(onUnmanageNamespace).to.have.been.calledOnce;
102+
});
103+
104+
it('Unmanage btn is disabled when the action is in progress', async function () {
105+
const onUnmanageNamespace = Sinon.spy();
106+
await renderWithProps({ onUnmanageNamespace, isUnmanagingNamespace: true });
107+
108+
const btn = await screen.findByTestId<HTMLButtonElement>(
109+
'unmanage-collection-button'
110+
);
111+
expect(btn).to.be.visible;
112+
expect(btn.getAttribute('aria-disabled')).to.equal('true');
113+
114+
userEvent.click(btn);
115+
116+
expect(onUnmanageNamespace).not.to.have.been.called;
117+
});
118+
});

packages/compass-global-writes/src/components/states/shard-key-mismatch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const getRequestedShardKey = (
4444
isUnique: managedNamespace.isShardKeyUnique,
4545
});
4646

47-
interface ShardKeyMismatchProps {
47+
export interface ShardKeyMismatchProps {
4848
shardKey?: ShardKey;
4949
requestedShardKey?: ShardKey;
5050
namespace: string;

0 commit comments

Comments
 (0)