Skip to content

Commit

Permalink
436 utils folder typescript migration (#442)
Browse files Browse the repository at this point in the history
* refactor. migrated date_utils file to typescript, add tests

Signed-off-by: titanventura <aswath7862001@gmail.com>

* refactor. migrated other utils to typescript, add tests

Signed-off-by: titanventura <aswath7862001@gmail.com>

---------

Signed-off-by: titanventura <aswath7862001@gmail.com>
  • Loading branch information
titanventura authored Jan 22, 2024
1 parent ee780c1 commit eab8193
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {validateGitlabURL} from './regex_utils';
import {describe, expect, it} from "@jest/globals";

describe('validateGitlabURL should work as expected', () => {
it('Should return true for valid GitLab repository URL', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function validateGitlabURL(url) {
export function validateGitlabURL(url: string): boolean {
const gitlabRegexPattern = /https?:\/\/(www\.)?.*\/([\w.?-]+)\/([\w-]+)\/-\/([\w-]+)\/([\d-]+$)/g;
return gitlabRegexPattern.test(url);
}
13 changes: 13 additions & 0 deletions webapp/src/utils/url_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {describe, expect, it} from "@jest/globals";
import {isValidUrl} from "./url_utils";


describe('url_utils', () => {
it("should return true for a valid URL", () => {
expect(isValidUrl("https://mattermost.com")).toBe(true);
})

it.each(["abc@example.com", "xyz", "://example.com", "example.com"])("should return false for invalid URLs %s", (input) => {
expect(isValidUrl(input)).toBe(false);
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const isValidUrl = (urlString) => {
export const isValidUrl = (urlString: string): boolean => {
try {
return Boolean(new URL(urlString));
} catch (e) {
Expand Down
3 changes: 0 additions & 3 deletions webapp/src/utils/user_agent.js

This file was deleted.

25 changes: 25 additions & 0 deletions webapp/src/utils/user_agent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {describe, expect, it, jest} from "@jest/globals";
import {isDesktopApp} from "./user_agent";


describe("user_agent", () => {
it("should return true if 'Mattermost' and 'Electron' are in userAgent", () => {
jest.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue('Mattermost Electron')
expect(isDesktopApp()).toBe(true);
})

it("should return false if only 'Mattermost' present in userAgent", () => {
jest.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue('Mattermost')
expect(isDesktopApp()).toBe(false);
})

it("should return false if only 'Electron' present in userAgent", () => {
jest.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue('Electron')
expect(isDesktopApp()).toBe(false);
})

it("should return false if neither 'Mattermost' nor 'Electron' present in userAgent", () => {
jest.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue('Google chrome')
expect(isDesktopApp()).toBe(false);
})
})
4 changes: 4 additions & 0 deletions webapp/src/utils/user_agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const isDesktopApp = () => {
const userAgent = window.navigator.userAgent;
return userAgent.indexOf('Mattermost') !== -1 && userAgent.indexOf('Electron') !== -1
};
17 changes: 0 additions & 17 deletions webapp/src/utils/user_utils.js

This file was deleted.

0 comments on commit eab8193

Please sign in to comment.