Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #127

Merged
merged 2 commits into from
Oct 21, 2024
Merged

Next #127

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/release-candidate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Release Candidate

on:
pull_request:
types: [closed]
branches:
- develop

jobs:
release_candidate:
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Generate RC version
id: rc_version
run: |
# Get the latest tag
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")

# Extract version number and increment patch
version=$(echo $latest_tag | sed 's/^v//' | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')

# Generate RC version
rc_version="rc-v${version}-${{ github.run_number }}"

echo "RC_VERSION=$rc_version" >> $GITHUB_OUTPUT

# - name: Create Release Candidate
# uses: rymndhng/release-on-push-action@master
# with:
# bump_version_scheme: none
# tag_prefix: ""
# tag_name: ${{ steps.rc_version.outputs.RC_VERSION }}
# release_name: "Release Candidate ${{ steps.rc_version.outputs.RC_VERSION }}"
# release_body: |
# This is a release candidate created from the develop branch.
# RC Version: ${{ steps.rc_version.outputs.RC_VERSION }}
#
# Changes in this release candidate:
# ${{ github.event.pull_request.title }}
#
# For full changes, please see the pull request: ${{ github.event.pull_request.html_url }}

outputs:
rc_version: ${{ steps.rc_version.outputs.RC_VERSION }}
9 changes: 6 additions & 3 deletions packages/javascript-sdk/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ export function getUtmParams(): Record<string, string> {

export function parseQueryString(queryString: string): Record<string, string> {
const params: Record<string, string> = {};
const queries = queryString.substring(1).split('&');
// Remove the leading '?' if present
const queries = queryString.replace(/^\?/, '').split('&');

for (let i = 0; i < queries.length; i++) {
const pair = queries[i].split('=');
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
if (pair[0] !== '') {
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
}

return params;
Expand All @@ -56,7 +59,7 @@ export function isString(value: any): boolean {
}

export function isObject(value: any): boolean {
return value && typeof value === 'object' && value.constructor === Object;
return value !== null && typeof value === 'object' && value.constructor === Object;
}


Expand Down
106 changes: 0 additions & 106 deletions packages/javascript-sdk/test/unit/core/server-side-client.test.ts

This file was deleted.

161 changes: 161 additions & 0 deletions packages/javascript-sdk/test/unit/utils/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
generateId,
isValidEmail,
debounce,
getUtmParams,
parseQueryString,
isString,
isObject,
parseLogLevel
} from '../../../src/utils/helpers';
import { LogLevel } from '../../../src/utils/logger';
import * as commonUtils from '../../../src/utils/common';

// Mock dependencies
vi.mock('../../../src/utils/common', () => ({
generateRandom: vi.fn()
}));

describe('Helper Functions', () => {
describe('generateId', () => {
it('should call generateRandom with 10', () => {
generateId();
expect(commonUtils.generateRandom).toHaveBeenCalledWith(10);
});
});

describe('isValidEmail', () => {
it('should return true for valid emails', () => {
expect(isValidEmail('[email protected]')).toBe(true);
expect(isValidEmail('[email protected]')).toBe(true);
});

it('should return false for invalid emails', () => {
expect(isValidEmail('notanemail')).toBe(false);
expect(isValidEmail('missing@tld')).toBe(false);
expect(isValidEmail('@missingusername.com')).toBe(false);
});
});

describe('debounce', () => {
beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it('should debounce function calls', () => {
const func = vi.fn();
const debouncedFunc = debounce(func, 1000);

debouncedFunc();
debouncedFunc();
debouncedFunc();

expect(func).not.toHaveBeenCalled();

vi.runAllTimers();

expect(func).toHaveBeenCalledTimes(1);
});
});

describe('getUtmParams', () => {
const originalWindow = global.window;

beforeEach(() => {
// @ts-ignore
delete global.window;
// @ts-ignore
global.window = { location: { search: '' } };
});

afterEach(() => {
global.window = originalWindow;
});

it('should extract UTM params from URL', () => {
global.window.location.search = '?utm_source=test&utm_medium=email&utm_campaign=summer';
expect(getUtmParams()).toEqual({
source: 'test',
medium: 'email',
campaign: 'summer'
});
});

it('should return an empty object if no UTM params', () => {
global.window.location.search = '?param1=value1&param2=value2';
expect(getUtmParams()).toEqual({});
});
});

describe('parseQueryString', () => {
it('should parse query string correctly', () => {
const queryString = '?param1=value1&param2=value2&param3=value%20with%20spaces';
expect(parseQueryString(queryString)).toEqual({
param1: 'value1',
param2: 'value2',
param3: 'value with spaces'
});
});

it('should handle empty query string', () => {
expect(parseQueryString('')).toEqual({});
});
});

describe('isString', () => {
it('should return true for strings', () => {
expect(isString('test')).toBe(true);
expect(isString(new String('test'))).toBe(true);
});

it('should return false for non-strings', () => {
expect(isString(123)).toBe(false);
expect(isString({})).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
});
});

describe('isObject', () => {
it('should return true for plain objects', () => {
expect(isObject({})).toBe(true);
expect(isObject({ key: 'value' })).toBe(true);
});

it('should return false for non-objects and non-plain objects', () => {
expect(isObject(null)).toBe(false);
expect(isObject([])).toBe(false);
expect(isObject('string')).toBe(false);
expect(isObject(123)).toBe(false);
expect(isObject(new Date())).toBe(false);
});
});

describe('parseLogLevel', () => {
it('should parse valid log levels', () => {
expect(parseLogLevel('ERROR')).toBe(LogLevel.ERROR);
expect(parseLogLevel('WARN')).toBe(LogLevel.WARN);
expect(parseLogLevel('INFO')).toBe(LogLevel.INFO);
expect(parseLogLevel('DEBUG')).toBe(LogLevel.DEBUG);
});

it('should be case-insensitive', () => {
expect(parseLogLevel('error')).toBe(LogLevel.ERROR);
expect(parseLogLevel('WaRn')).toBe(LogLevel.WARN);
});

it('should return ERROR for null input', () => {
expect(parseLogLevel(null)).toBe(LogLevel.ERROR);
});

it('should return ERROR for invalid input', () => {
expect(parseLogLevel('INVALID')).toBe(LogLevel.ERROR);
expect(parseLogLevel('123')).toBe(LogLevel.ERROR);
});
});
});
Loading
Loading