2020-10-26 13:20:17 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { CoreApp } from '@services/app';
|
|
|
|
import { CoreTextUtilsProvider } from '@services/utils/text';
|
2021-06-07 17:17:35 +02:00
|
|
|
import { DomSanitizer } from '@singletons';
|
2020-10-26 13:20:17 +01:00
|
|
|
|
2021-06-07 17:17:35 +02:00
|
|
|
import { mockSingleton } from '@/testing/utils';
|
2020-10-26 13:20:17 +01:00
|
|
|
|
|
|
|
describe('CoreTextUtilsProvider', () => {
|
|
|
|
|
|
|
|
const config = { platform: 'android' };
|
|
|
|
let textUtils: CoreTextUtilsProvider;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-03-15 14:31:00 +01:00
|
|
|
mockSingleton(CoreApp, [], { isAndroid: () => config.platform === 'android' });
|
2021-06-07 17:17:35 +02:00
|
|
|
mockSingleton(DomSanitizer, [], { bypassSecurityTrustUrl: url => url });
|
2020-10-26 13:20:17 +01:00
|
|
|
|
2021-06-07 17:17:35 +02:00
|
|
|
textUtils = new CoreTextUtilsProvider();
|
2020-10-26 13:20:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('adds ending slashes', () => {
|
|
|
|
const originalUrl = 'https://moodle.org';
|
|
|
|
const url = textUtils.addEndingSlash(originalUrl);
|
|
|
|
|
|
|
|
expect(url).toEqual('https://moodle.org/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('doesn\'t add duplicated ending slashes', () => {
|
|
|
|
const originalUrl = 'https://moodle.org/';
|
|
|
|
const url = textUtils.addEndingSlash(originalUrl);
|
|
|
|
|
|
|
|
expect(url).toEqual('https://moodle.org/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds address URL for Android platforms', () => {
|
|
|
|
// Arrange
|
|
|
|
const address = 'Moodle Spain HQ';
|
|
|
|
|
|
|
|
config.platform = 'android';
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const url = textUtils.buildAddressURL(address);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(url).toEqual('geo:0,0?q=Moodle%20Spain%20HQ');
|
|
|
|
|
2021-06-07 17:17:35 +02:00
|
|
|
expect(DomSanitizer.bypassSecurityTrustUrl).toHaveBeenCalled();
|
2021-03-02 11:41:04 +01:00
|
|
|
expect(CoreApp.isAndroid).toHaveBeenCalled();
|
2020-10-26 13:20:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('builds address URL for non-Android platforms', () => {
|
|
|
|
// Arrange
|
|
|
|
const address = 'Moodle Spain HQ';
|
|
|
|
|
|
|
|
config.platform = 'ios';
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const url = textUtils.buildAddressURL(address);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(url).toEqual('http://maps.google.com?q=Moodle%20Spain%20HQ');
|
|
|
|
|
2021-06-07 17:17:35 +02:00
|
|
|
expect(DomSanitizer.bypassSecurityTrustUrl).toHaveBeenCalled();
|
2021-03-02 11:41:04 +01:00
|
|
|
expect(CoreApp.isAndroid).toHaveBeenCalled();
|
2020-10-26 13:20:17 +01:00
|
|
|
});
|
|
|
|
|
2021-01-19 16:28:37 +01:00
|
|
|
it('matches glob patterns', () => {
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar', '/foo/bar')).toBe(true);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar', '/foo/bar/')).toBe(false);
|
|
|
|
expect(textUtils.matchesGlob('/foo', '/foo/*')).toBe(false);
|
|
|
|
expect(textUtils.matchesGlob('/foo/', '/foo/*')).toBe(true);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar', '/foo/*')).toBe(true);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/', '/foo/*')).toBe(false);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/baz', '/foo/*')).toBe(false);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/baz', '/foo/**')).toBe(true);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/baz/', '/foo/**')).toBe(true);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/baz', '**/baz')).toBe(true);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/baz', '**/bar')).toBe(false);
|
|
|
|
expect(textUtils.matchesGlob('/foo/bar/baz', '/foo/ba?/ba?')).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-06-01 12:34:51 +02:00
|
|
|
it('replaces arguments', () => {
|
|
|
|
// Arrange
|
|
|
|
const url = 'http://campus.edu?device={{device}}&version={{version}}';
|
|
|
|
const replacements = {
|
|
|
|
device: 'iPhone or iPad',
|
|
|
|
version: '1.2.3',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
const replaced = textUtils.replaceArguments(url, replacements, 'uri');
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(replaced).toEqual('http://campus.edu?device=iPhone%20or%20iPad&version=1.2.3');
|
|
|
|
});
|
|
|
|
|
2020-10-26 13:20:17 +01:00
|
|
|
});
|