|
| 1 | +import { bufferTime, from, of } from 'rxjs'; |
| 2 | +import * as models from '../../../shared/models'; |
| 3 | +import { services } from '../../../shared/services'; |
| 4 | +import * as AppUtils from '../utils'; |
| 5 | + |
| 6 | +// Import the function directly from the file |
| 7 | +// Note: This is a bit of a hack for testing purposes |
| 8 | +import { loadApplications } from './applications-list'; |
| 9 | + |
| 10 | +jest.mock('../../../shared/services', () => ({ |
| 11 | + applications: { |
| 12 | + list: jest.fn(), |
| 13 | + watch: jest.fn() |
| 14 | + }, |
| 15 | + viewPreferences: { |
| 16 | + getPreferences: jest.fn() |
| 17 | + } |
| 18 | +})); |
| 19 | + |
| 20 | +jest.mock('../utils', () => ({ |
| 21 | + appInstanceName: jest.fn((app) => app.metadata.name), |
| 22 | + handlePageVisibility: jest.fn((callback) => callback()) |
| 23 | +})); |
| 24 | + |
| 25 | +describe('loadApplications', () => { |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should load applications and handle updates correctly', (done) => { |
| 31 | + // Mock initial applications list |
| 32 | + const initialApps = { |
| 33 | + items: [ |
| 34 | + { metadata: { name: 'app1', namespace: 'default' } }, |
| 35 | + { metadata: { name: 'app2', namespace: 'default' } } |
| 36 | + ], |
| 37 | + metadata: { resourceVersion: '123' } |
| 38 | + }; |
| 39 | + |
| 40 | + // Mock watch events |
| 41 | + const watchEvents = [ |
| 42 | + { |
| 43 | + type: 'MODIFIED', |
| 44 | + application: { metadata: { name: 'app1', namespace: 'default', annotations: { updated: 'true' } } } |
| 45 | + }, |
| 46 | + { |
| 47 | + type: 'ADDED', |
| 48 | + application: { metadata: { name: 'app3', namespace: 'default' } } |
| 49 | + }, |
| 50 | + { |
| 51 | + type: 'DELETED', |
| 52 | + application: { metadata: { name: 'app2', namespace: 'default' } } |
| 53 | + } |
| 54 | + ]; |
| 55 | + |
| 56 | + // Setup mocks |
| 57 | + (services.applications.list as jest.Mock).mockReturnValue(Promise.resolve(initialApps)); |
| 58 | + (services.applications.watch as jest.Mock).mockReturnValue(of(...watchEvents)); |
| 59 | + |
| 60 | + // Call the function |
| 61 | + const projects = ['project1']; |
| 62 | + const appNamespace = 'default'; |
| 63 | + const result = loadApplications(projects, appNamespace); |
| 64 | + |
| 65 | + // Subscribe to the result |
| 66 | + let callCount = 0; |
| 67 | + result.subscribe(apps => { |
| 68 | + callCount++; |
| 69 | + |
| 70 | + if (callCount === 1) { |
| 71 | + // First emission should be the initial apps |
| 72 | + expect(apps.length).toBe(2); |
| 73 | + expect(apps[0].metadata.name).toBe('app1'); |
| 74 | + expect(apps[1].metadata.name).toBe('app2'); |
| 75 | + } else if (callCount === 2) { |
| 76 | + // Second emission should include all updates |
| 77 | + expect(apps.length).toBe(2); // app1 (modified) and app3 (added), app2 (deleted) |
| 78 | + |
| 79 | + // Check app1 was updated |
| 80 | + const app1 = apps.find(app => app.metadata.name === 'app1'); |
| 81 | + expect(app1).toBeDefined(); |
| 82 | + expect(app1.metadata.annotations.updated).toBe('true'); |
| 83 | + |
| 84 | + // Check app3 was added |
| 85 | + const app3 = apps.find(app => app.metadata.name === 'app3'); |
| 86 | + expect(app3).toBeDefined(); |
| 87 | + |
| 88 | + // Check app2 was deleted |
| 89 | + const app2 = apps.find(app => app.metadata.name === 'app2'); |
| 90 | + expect(app2).toBeUndefined(); |
| 91 | + |
| 92 | + done(); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + // Verify the mocks were called correctly |
| 97 | + expect(services.applications.list).toHaveBeenCalledWith(projects, { |
| 98 | + appNamespace, |
| 99 | + fields: expect.any(Array) |
| 100 | + }); |
| 101 | + |
| 102 | + expect(services.applications.watch).toHaveBeenCalledWith( |
| 103 | + { projects, resourceVersion: '123' }, |
| 104 | + { fields: expect.any(Array) } |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle empty update batches correctly', (done) => { |
| 109 | + // Mock initial applications list |
| 110 | + const initialApps = { |
| 111 | + items: [ |
| 112 | + { metadata: { name: 'app1', namespace: 'default' } } |
| 113 | + ], |
| 114 | + metadata: { resourceVersion: '123' } |
| 115 | + }; |
| 116 | + |
| 117 | + // Setup mocks |
| 118 | + (services.applications.list as jest.Mock).mockReturnValue(Promise.resolve(initialApps)); |
| 119 | + (services.applications.watch as jest.Mock).mockReturnValue(of([])); // Empty update batch |
| 120 | + |
| 121 | + // Call the function |
| 122 | + const projects = ['project1']; |
| 123 | + const appNamespace = 'default'; |
| 124 | + const result = loadApplications(projects, appNamespace); |
| 125 | + |
| 126 | + // Subscribe to the result |
| 127 | + let callCount = 0; |
| 128 | + result.subscribe(apps => { |
| 129 | + callCount++; |
| 130 | + |
| 131 | + if (callCount === 1) { |
| 132 | + // First emission should be the initial apps |
| 133 | + expect(apps.length).toBe(1); |
| 134 | + expect(apps[0].metadata.name).toBe('app1'); |
| 135 | + done(); |
| 136 | + } |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments