|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import {beforeEach, describe, expect, it, vi, Mock} from 'vitest'; |
| 20 | +import AsgardeoNextClient from '../../../AsgardeoNextClient'; |
| 21 | +import updateOrganizationAction from '../updateOrganizationAction'; |
| 22 | + |
| 23 | +vi.mock('../../../AsgardeoNextClient', () => ({ |
| 24 | + default: { |
| 25 | + getInstance: vi.fn(), |
| 26 | + }, |
| 27 | +})); |
| 28 | + |
| 29 | +describe('updateOrganizationAction', () => { |
| 30 | + type ActionResult = Awaited<ReturnType<typeof updateOrganizationAction>>; |
| 31 | + |
| 32 | + const client: {updateOrganization: Mock} = {updateOrganization: vi.fn()}; |
| 33 | + const operations: Array<{operation: 'REPLACE'; path: string; value: string}> = [ |
| 34 | + {operation: 'REPLACE', path: '/name', value: 'Acme Inc.'}, |
| 35 | + ]; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks(); |
| 39 | + (AsgardeoNextClient.getInstance as unknown as Mock).mockReturnValue(client); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns the updated organization when the update succeeds', async () => { |
| 43 | + const organization: Record<string, unknown> = {id: 'org-1', name: 'Acme Inc.'}; |
| 44 | + |
| 45 | + client.updateOrganization.mockResolvedValue(organization); |
| 46 | + |
| 47 | + const result: ActionResult = await updateOrganizationAction('org-1', operations, 'session-1'); |
| 48 | + |
| 49 | + expect(client.updateOrganization).toHaveBeenCalledWith('org-1', operations, 'session-1'); |
| 50 | + expect(result).toEqual({data: {organization}, error: null, success: true}); |
| 51 | + }); |
| 52 | + |
| 53 | + it('reports the failure reason instead of throwing when the update fails', async () => { |
| 54 | + client.updateOrganization.mockRejectedValue(new Error('Failed to update the organization org-1: forbidden')); |
| 55 | + |
| 56 | + const result: ActionResult = await updateOrganizationAction('org-1', operations); |
| 57 | + |
| 58 | + expect(result.success).toBe(false); |
| 59 | + expect(result.error).toBe('Failed to update the organization org-1: forbidden'); |
| 60 | + expect(result.data.organization).toBeUndefined(); |
| 61 | + }); |
| 62 | +}); |
0 commit comments