Skip to content

Commit

Permalink
Add serializer unit tests
Browse files Browse the repository at this point in the history
Test cases were missing for multi-namespace types. Added those
tests. No functionality changes.
  • Loading branch information
jportner committed Dec 2, 2020
1 parent d47d578 commit 1b0165b
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/core/server/saved_objects/serialization/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,99 @@ describe('#isRawSavedObject', () => {
});
});

describe('multi-namespace type with a namespace', () => {
test('is true if the id is prefixed with type and the type matches', () => {
expect(
multiNamespaceSerializer.isRawSavedObject({
_id: 'hello:world',
_source: {
type: 'hello',
hello: {},
namespace: 'foo',
},
})
).toBeTruthy();
});

test('is false if the id is not prefixed', () => {
expect(
multiNamespaceSerializer.isRawSavedObject({
_id: 'world',
_source: {
type: 'hello',
hello: {},
namespace: 'foo',
},
})
).toBeFalsy();
});

test('is false if the id is prefixed with type and namespace', () => {
expect(
multiNamespaceSerializer.isRawSavedObject({
_id: 'foo:hello:world',
_source: {
type: 'hello',
hello: {},
namespace: 'foo',
},
})
).toBeFalsy();
});

test(`is false if the type prefix omits the :`, () => {
expect(
namespaceAgnosticSerializer.isRawSavedObject({
_id: 'helloworld',
_source: {
type: 'hello',
hello: {},
namespace: 'foo',
},
})
).toBeFalsy();
});

test('is false if the type attribute is missing', () => {
expect(
multiNamespaceSerializer.isRawSavedObject({
_id: 'hello:world',
_source: {
hello: {},
namespace: 'foo',
} as any,
})
).toBeFalsy();
});

test('is false if the type attribute does not match the id', () => {
expect(
multiNamespaceSerializer.isRawSavedObject({
_id: 'hello:world',
_source: {
type: 'jam',
jam: {},
hello: {},
namespace: 'foo',
},
})
).toBeFalsy();
});

test('is false if there is no [type] attribute', () => {
expect(
multiNamespaceSerializer.isRawSavedObject({
_id: 'hello:world',
_source: {
type: 'hello',
jam: {},
namespace: 'foo',
},
})
).toBeFalsy();
});
});

describe('namespace-agnostic type with a namespace', () => {
test('is true if the id is prefixed with type and the type matches', () => {
expect(
Expand Down Expand Up @@ -1087,6 +1180,18 @@ describe('#generateRawId', () => {
});
});

describe('multi-namespace type with a namespace', () => {
test(`generates an id if none is specified and doesn't prefix namespace`, () => {
const id = multiNamespaceSerializer.generateRawId('foo', 'goodbye');
expect(id).toMatch(/^goodbye\:[\w-]+$/);
});

test(`uses the id that is specified and doesn't prefix the namespace`, () => {
const id = multiNamespaceSerializer.generateRawId('foo', 'hello', 'world');
expect(id).toEqual('hello:world');
});
});

describe('namespace-agnostic type with a namespace', () => {
test(`generates an id if none is specified and doesn't prefix namespace`, () => {
const id = namespaceAgnosticSerializer.generateRawId('foo', 'goodbye');
Expand Down

0 comments on commit 1b0165b

Please sign in to comment.