Skip to content

Commit cf49a77

Browse files
committed
[ML] Fix type guard and add jest tests.
1 parent 9cc9719 commit cf49a77

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { isNodes } from './transforms_nodes';
9+
10+
describe('Transform: Nodes API endpoint', () => {
11+
test('isNodes()', () => {
12+
expect(isNodes(undefined)).toBe(false);
13+
expect(isNodes({})).toBe(false);
14+
expect(isNodes({ nodeId: {} })).toBe(false);
15+
expect(isNodes({ nodeId: { someAttribute: {} } })).toBe(false);
16+
expect(isNodes({ nodeId: { attributes: {} } })).toBe(false);
17+
expect(
18+
isNodes({
19+
nodeId1: { attributes: { someAttribute: true } },
20+
nodeId2: { someAttribute: 'asdf' },
21+
})
22+
).toBe(false);
23+
24+
expect(isNodes({ nodeId: { attributes: { someAttribute: true } } })).toBe(true);
25+
expect(
26+
isNodes({
27+
nodeId1: { attributes: { someAttribute: true } },
28+
nodeId2: { attributes: { 'transform.node': 'true' } },
29+
})
30+
).toBe(true);
31+
});
32+
});

x-pack/plugins/transform/server/routes/api/transforms_nodes.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ import { wrapError, wrapEsError } from './error_utils';
1515

1616
interface NodesAttributes {
1717
attributes: {
18-
'transform.node': string;
18+
'transform.node'?: string;
1919
};
2020
}
2121
type Nodes = Record<string, NodesAttributes>;
2222

23-
const isNodes = (arg: unknown): arg is Nodes => {
23+
export const isNodes = (arg: unknown): arg is Nodes => {
2424
return (
2525
isPopulatedObject(arg) &&
2626
Object.values(arg).every(
2727
(node) =>
2828
isPopulatedObject(node) &&
2929
{}.hasOwnProperty.call(node, 'attributes') &&
30-
isPopulatedObject(node.attributes) &&
31-
{}.hasOwnProperty.call(node.attributes, 'transform.node')
30+
isPopulatedObject(node.attributes)
3231
)
3332
);
3433
};

0 commit comments

Comments
 (0)