|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { expect } from 'chai'; |
| 8 | +import { Connection, Org } from '@salesforce/core'; |
| 9 | +import sinon from 'sinon'; |
| 10 | +import { |
| 11 | + replaceSpacesAndSpecialChars, |
| 12 | + hasSpacesOrSpecialChars, |
| 13 | + ExperienceSite, |
| 14 | +} from '../../src/shared/experience/expSite.js'; |
| 15 | + |
| 16 | +describe('replaceSpacesAndSpecialChars', () => { |
| 17 | + it('should replace spaces and special characters with underscores', () => { |
| 18 | + const input = 'site#name@with-special%chars with spaces'; |
| 19 | + const expectedOutput = 'site_name_with_special_chars_with_spaces'; |
| 20 | + const output = replaceSpacesAndSpecialChars(input); |
| 21 | + expect(output).to.equal(expectedOutput); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('hasSpacesOrSpecialChars', () => { |
| 26 | + it('should return true if the input string has spaces', () => { |
| 27 | + const input = 'Hello World'; |
| 28 | + const output = hasSpacesOrSpecialChars(input); |
| 29 | + expect(output).to.be.true; |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return true if the input string has special characters', () => { |
| 33 | + const input = 'Hello, @#'; |
| 34 | + const output = hasSpacesOrSpecialChars(input); |
| 35 | + expect(output).to.be.true; |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return false if the input string has neither spaces nor special characters', () => { |
| 39 | + const input = 'HelloWorld'; |
| 40 | + const output = hasSpacesOrSpecialChars(input); |
| 41 | + expect(output).to.be.false; |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('getRemoteMetadata', () => { |
| 46 | + it('should return remote metadata when it exists', async () => { |
| 47 | + const org = new Org(); |
| 48 | + const siteName = 'site@with#special-chars'; |
| 49 | + const experienceSite = new ExperienceSite(org, siteName); |
| 50 | + |
| 51 | + // Create a mock Connection instance using sinon |
| 52 | + const mockConnection = sinon.createStubInstance(Connection); |
| 53 | + |
| 54 | + // Configure the mock to return the desired result when calling query |
| 55 | + mockConnection.query.resolves({ |
| 56 | + done: true, |
| 57 | + totalSize: 1, |
| 58 | + records: [ |
| 59 | + { |
| 60 | + Name: 'MRT_experience_00DSG00000ECBfZ_0DMSG000001CfA6_site_with_special_chars_10-30_12-47', |
| 61 | + LastModifiedDate: '2024-11-12', |
| 62 | + }, |
| 63 | + ], |
| 64 | + }); |
| 65 | + |
| 66 | + // Replace the original connection with the mocked connection |
| 67 | + org.getConnection = () => mockConnection; |
| 68 | + |
| 69 | + const remoteMetadata = await experienceSite.getRemoteMetadata(); |
| 70 | + |
| 71 | + // Check if the called query matches the expected pattern |
| 72 | + const calledQuery = mockConnection.query.args[0][0]; |
| 73 | + const expectedPattern = |
| 74 | + /SELECT Name, LastModifiedDate FROM StaticResource WHERE Name LIKE 'MRT_experience_%_site_with_special_chars/; |
| 75 | + expect(calledQuery).to.match(expectedPattern); |
| 76 | + |
| 77 | + expect(remoteMetadata).to.deep.equal({ |
| 78 | + bundleName: 'MRT_experience_00DSG00000ECBfZ_0DMSG000001CfA6_site_with_special_chars_10-30_12-47', |
| 79 | + bundleLastModified: '2024-11-12', |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments