Skip to content

Commit 9986033

Browse files
makdenissmakdeniss
authored andcommitted
fix: fix tests (#335)
1 parent 2647ee7 commit 9986033

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

projects/lib/services/resource/resource.service.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,44 @@ describe('ResourceService', () => {
565565
});
566566

567567
describe('update', () => {
568+
it('should strip __typename recursively from update payload', (done) => {
569+
const dirtyResource: any = {
570+
metadata: { name: 'test-name', __typename: 'Meta' },
571+
spec: {
572+
__typename: 'Spec',
573+
items: [
574+
{ key: 'a', __typename: 'Item' },
575+
{ key: 'b', nested: { foo: 'bar', __typename: 'Nested' } },
576+
],
577+
map: {
578+
one: { val: 1, __typename: 'Val' },
579+
two: [{ x: 1, __typename: 'X' }, { y: 2 }],
580+
},
581+
},
582+
};
583+
584+
mockApollo.mutate.mockReturnValue(
585+
of({ data: { __typename: 'TestKind' } }),
586+
);
587+
588+
service
589+
.update(dirtyResource, resourceDefinition, namespacedNodeContext)
590+
.subscribe(() => {
591+
const mutateCall = mockApollo.mutate.mock.calls[0][0];
592+
const passedObject = mutateCall.variables.object;
593+
expect(passedObject).toEqual({
594+
metadata: { name: 'test-name' },
595+
spec: {
596+
items: [{ key: 'a' }, { key: 'b', nested: { foo: 'bar' } }],
597+
map: {
598+
one: { val: 1 },
599+
two: [{ x: 1 }, { y: 2 }],
600+
},
601+
},
602+
});
603+
done();
604+
});
605+
});
568606
it('should update resource', (done) => {
569607
mockApollo.mutate.mockReturnValue(
570608
of({ data: { __typename: 'TestKind' } }),
@@ -641,6 +679,33 @@ describe('ResourceService', () => {
641679
});
642680
});
643681

682+
describe('stripTypename (private)', () => {
683+
it('should return primitives as-is', () => {
684+
// @ts-ignore accessing private method for test
685+
expect((service as any).stripTypename(42)).toBe(42);
686+
// @ts-ignore
687+
expect((service as any).stripTypename('x')).toBe('x');
688+
// @ts-ignore
689+
expect((service as any).stripTypename(null)).toBe(null);
690+
// @ts-ignore
691+
expect((service as any).stripTypename(undefined)).toBe(undefined);
692+
});
693+
694+
it('should clean nested objects and arrays', () => {
695+
const input = {
696+
__typename: 'Root',
697+
a: { __typename: 'A', b: 1 },
698+
arr: [
699+
{ __typename: 'X', v: 1 },
700+
[{ __typename: 'Y', z: 2 }, 3],
701+
],
702+
} as any;
703+
// @ts-ignore
704+
const output = (service as any).stripTypename(input);
705+
expect(output).toEqual({ a: { b: 1 }, arr: [{ v: 1 }, [{ z: 2 }, 3]] });
706+
});
707+
});
708+
644709
describe('readAccountInfo', () => {
645710
it('should read account info', (done) => {
646711
const ca = 'cert-data';

projects/lib/services/resource/resource.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export class ResourceService {
377377

378378
private stripTypename<T>(value: T): T {
379379
if (Array.isArray(value)) {
380-
return value.map(this.stripTypename) as unknown as T;
380+
return (value as unknown as any[]).map((v) => this.stripTypename(v)) as unknown as T;
381381
}
382382
if (value && typeof value === 'object') {
383383
const { __typename, ...rest } = value as Record<string, unknown>;

0 commit comments

Comments
 (0)