|
| 1 | +import { ApolloFactory } from './apollo-factory'; |
| 2 | +import { ResourceService } from './resource.service'; |
1 | 3 | import { TestBed } from '@angular/core/testing'; |
2 | 4 | import { LuigiCoreService } from '@openmfp/portal-ui-lib'; |
3 | 5 | import { mock } from 'jest-mock-extended'; |
4 | 6 | import { of, throwError } from 'rxjs'; |
5 | | -import { ApolloFactory } from './apollo-factory'; |
6 | | -import { ResourceService } from './resource.service'; |
7 | 7 |
|
8 | 8 | describe('ResourceService', () => { |
9 | 9 | let service: ResourceService; |
@@ -564,6 +564,122 @@ describe('ResourceService', () => { |
564 | 564 | }); |
565 | 565 | }); |
566 | 566 |
|
| 567 | + 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 | + }); |
| 606 | + it('should update resource', (done) => { |
| 607 | + mockApollo.mutate.mockReturnValue( |
| 608 | + of({ data: { __typename: 'TestKind' } }), |
| 609 | + ); |
| 610 | + service |
| 611 | + .update(resource, resourceDefinition, namespacedNodeContext) |
| 612 | + .subscribe(() => { |
| 613 | + expect(mockApollo.mutate).toHaveBeenCalled(); |
| 614 | + done(); |
| 615 | + }); |
| 616 | + }); |
| 617 | + |
| 618 | + it('should update namespaced resource', (done) => { |
| 619 | + mockApollo.mutate.mockReturnValue( |
| 620 | + of({ data: { __typename: 'TestKind' } }), |
| 621 | + ); |
| 622 | + |
| 623 | + service |
| 624 | + .update(resource, resourceDefinition, namespacedNodeContext) |
| 625 | + .subscribe(() => { |
| 626 | + expect(mockApollo.mutate).toHaveBeenCalledWith({ |
| 627 | + mutation: expect.anything(), |
| 628 | + fetchPolicy: 'no-cache', |
| 629 | + variables: { |
| 630 | + name: resource.metadata.name, |
| 631 | + object: resource, |
| 632 | + namespace: namespacedNodeContext.namespaceId, |
| 633 | + }, |
| 634 | + }); |
| 635 | + done(); |
| 636 | + }); |
| 637 | + }); |
| 638 | + |
| 639 | + it('should update cluster resource', (done) => { |
| 640 | + mockApollo.mutate.mockReturnValue( |
| 641 | + of({ data: { __typename: 'TestKind' } }), |
| 642 | + ); |
| 643 | + |
| 644 | + service |
| 645 | + .update(resource, resourceDefinition, clusterScopeNodeContext) |
| 646 | + .subscribe(() => { |
| 647 | + expect(mockApollo.mutate).toHaveBeenCalledWith({ |
| 648 | + mutation: expect.anything(), |
| 649 | + fetchPolicy: 'no-cache', |
| 650 | + variables: { |
| 651 | + name: resource.metadata.name, |
| 652 | + object: resource, |
| 653 | + }, |
| 654 | + }); |
| 655 | + done(); |
| 656 | + }); |
| 657 | + }); |
| 658 | + |
| 659 | + it('should handle update error', (done) => { |
| 660 | + const error = new Error('fail'); |
| 661 | + mockApollo.mutate.mockReturnValue(throwError(() => error)); |
| 662 | + console.error = jest.fn(); |
| 663 | + |
| 664 | + service |
| 665 | + .update(resource, resourceDefinition, clusterScopeNodeContext) |
| 666 | + .subscribe({ |
| 667 | + error: () => { |
| 668 | + expect(console.error).toHaveBeenCalledWith( |
| 669 | + 'Error executing GraphQL query.', |
| 670 | + error, |
| 671 | + ); |
| 672 | + expect(mockLuigiCoreService.showAlert).toHaveBeenCalledWith({ |
| 673 | + text: 'fail', |
| 674 | + type: 'error', |
| 675 | + }); |
| 676 | + done(); |
| 677 | + }, |
| 678 | + }); |
| 679 | + }); |
| 680 | + }); |
| 681 | + |
| 682 | + |
567 | 683 | describe('readAccountInfo', () => { |
568 | 684 | it('should read account info', (done) => { |
569 | 685 | const ca = 'cert-data'; |
|
0 commit comments