|
| 1 | +import { expect } from '@salesforce/command/lib/test'; |
| 2 | +import { Connection } from '@salesforce/core'; |
| 3 | +import sinon = require('sinon'); |
| 4 | +import { OrgPreferences } from '../../src/utils/orgpreferences'; |
| 5 | + |
| 6 | +describe('OrgPreferences', () => { |
| 7 | + let connection: Connection; |
| 8 | + let sandbox: sinon.SinonSandbox; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + sandbox = sinon.createSandbox(); |
| 12 | + connection = { |
| 13 | + metadata: { |
| 14 | + update: sandbox.stub(), |
| 15 | + }, |
| 16 | + query: sandbox.stub(), |
| 17 | + } as unknown as Connection; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + sandbox.restore(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('enableOmniPreferences', () => { |
| 25 | + it('should successfully enable OmniStudio preferences', async () => { |
| 26 | + // Arrange |
| 27 | + const metadataUpdateStub = sandbox.stub().resolves(); |
| 28 | + connection.metadata.update = metadataUpdateStub; |
| 29 | + |
| 30 | + // Act |
| 31 | + await OrgPreferences.enableOmniPreferences(connection); |
| 32 | + |
| 33 | + // Assert |
| 34 | + expect(metadataUpdateStub.calledOnce).to.be.true; |
| 35 | + expect(metadataUpdateStub.firstCall.args[0]).to.equal('OmniStudioSettings'); |
| 36 | + expect(metadataUpdateStub.firstCall.args[1]).to.deep.equal([ |
| 37 | + { |
| 38 | + fullName: 'OmniStudio', |
| 39 | + disableRollbackFlagsPref: true, |
| 40 | + }, |
| 41 | + ]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should throw error when enabling preferences fails', async () => { |
| 45 | + // Arrange |
| 46 | + const error = new Error('Failed to update metadata'); |
| 47 | + connection.metadata.update = sandbox.stub().rejects(error); |
| 48 | + |
| 49 | + // Act & Assert |
| 50 | + try { |
| 51 | + await OrgPreferences.enableOmniPreferences(connection); |
| 52 | + expect.fail('Expected an error to be thrown'); |
| 53 | + } catch (err: unknown) { |
| 54 | + if (err instanceof Error) { |
| 55 | + expect(err.message).to.equal('Failed to enable disableRollbackFlagsPref: Failed to update metadata'); |
| 56 | + } else { |
| 57 | + expect.fail('Expected an Error object'); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('checkRollbackFlags', () => { |
| 64 | + it('should return empty array when no flags are enabled', async () => { |
| 65 | + // Arrange |
| 66 | + const queryResult = { |
| 67 | + records: [ |
| 68 | + { DeveloperName: 'RollbackIPChanges', Value: 'false' }, |
| 69 | + { DeveloperName: 'RollbackDRChanges', Value: 'false' }, |
| 70 | + { DeveloperName: 'RollbackOSChanges', Value: 'false' }, |
| 71 | + ], |
| 72 | + }; |
| 73 | + const queryStub = sandbox.stub().resolves(queryResult); |
| 74 | + connection.query = queryStub; |
| 75 | + |
| 76 | + // Act |
| 77 | + const result = await OrgPreferences.checkRollbackFlags(connection); |
| 78 | + |
| 79 | + // Assert |
| 80 | + expect(result).to.be.an('array').that.is.empty; |
| 81 | + expect(queryStub.calledOnce).to.be.true; |
| 82 | + expect(queryStub.firstCall.args[0]).to.include('SELECT DeveloperName, Value FROM OmniInteractionConfig'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return array of enabled flags', async () => { |
| 86 | + // Arrange |
| 87 | + const queryResult = { |
| 88 | + records: [ |
| 89 | + { DeveloperName: 'RollbackIPChanges', Value: 'true' }, |
| 90 | + { DeveloperName: 'RollbackDRChanges', Value: 'false' }, |
| 91 | + { DeveloperName: 'RollbackOSChanges', Value: 'true' }, |
| 92 | + ], |
| 93 | + }; |
| 94 | + const queryStub = sandbox.stub().resolves(queryResult); |
| 95 | + connection.query = queryStub; |
| 96 | + |
| 97 | + // Act |
| 98 | + const result = await OrgPreferences.checkRollbackFlags(connection); |
| 99 | + |
| 100 | + // Assert |
| 101 | + expect(result).to.deep.equal(['RollbackIPChanges', 'RollbackOSChanges']); |
| 102 | + expect(queryStub.calledOnce).to.be.true; |
| 103 | + }); |
| 104 | + |
| 105 | + it('should throw error when checking flags fails', async () => { |
| 106 | + // Arrange |
| 107 | + const error = new Error('Failed to query'); |
| 108 | + connection.query = sandbox.stub().rejects(error); |
| 109 | + |
| 110 | + // Act & Assert |
| 111 | + try { |
| 112 | + await OrgPreferences.checkRollbackFlags(connection); |
| 113 | + expect.fail('Expected an error to be thrown'); |
| 114 | + } catch (err: unknown) { |
| 115 | + if (err instanceof Error) { |
| 116 | + expect(err.message).to.equal('Failed to check rollback flags: Failed to query'); |
| 117 | + } else { |
| 118 | + expect.fail('Expected an Error object'); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments