Skip to content

Commit

Permalink
fix: fixed the template utility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hirad-deriv committed Jul 25, 2023
1 parent 9bd66e7 commit bb07c67
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
15 changes: 0 additions & 15 deletions packages/cfd/src/_common/__tests__/utility.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/cfd/src/_common/__tests__/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { template } from '../utility';

describe('Utility', () => {
describe('.template()', () => {
it('works as expected', () => {
expect(template('abc [_1] abc', ['2'])).toBe('abc 2 abc');
expect(template('[_1] [_2]', ['1', '2'])).toBe('1 2');
expect(template('[_1] [_1]', ['1'])).toBe('1 1');
});

it('does not replace twice', () => {
expect(template('[_1] [_2]', ['[_2]', 'abc'])).toBe('[_2] abc');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { PromiseClass } from '../utility';
import type { TCommonStore } from '@deriv/stores/types';

const ServerTime = (() => {
let clock_started = false;
const pending = new PromiseClass();
let common_store;
let common_store: TCommonStore;

const init = store => {
const init = (store: TCommonStore) => {
if (!clock_started) {
common_store = store;
pending.resolve(common_store.server_time);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const template = (string, content) => {
const template = (string: string, content: string[]) => {
let to_replace = content;
if (content && !Array.isArray(content)) {
to_replace = [content];
Expand All @@ -13,29 +13,35 @@ const template = (string, content) => {
* @param {Object} attributes: all the attributes to assign, e.g. { id: '...', class: '...', html: '...', ... }
* @return the created DOM element
*/
const createElement = (tag_name, attributes = {}) => {
const createElement = <K extends keyof HTMLElementTagNameMap>(
tag_name: K,
attributes: ElementCreationOptions
): HTMLElement => {
const el = document.createElement(tag_name);
Object.keys(attributes).forEach(attr => {
const value = attributes[attr];
Object.keys(attributes).forEach((attr: string) => {
const value = attr;
if (attr === 'text') {
el.textContent = value;
} else if (attr === 'html') {
el.html(value);
el.innerHTML = value;
} else {
el.setAttribute(attr, value);
}
});
return el;
};

let static_hash;
let static_hash: string;
const getStaticHash = () => {
static_hash =
static_hash || (document.querySelector('script[src*="main"]').getAttribute('src') || '').split('.')[1];
static_hash || (document.querySelector('script[src*="main"]')?.getAttribute('src') || '').split('.')[1];
return static_hash;
};

class PromiseClass {
promise: Promise<unknown>;
reject?: (reason?: any) => void;
resolve!: (value: unknown) => void;
constructor() {
this.promise = new Promise((resolve, reject) => {
this.reject = reject;
Expand All @@ -44,9 +50,4 @@ class PromiseClass {
}
}

module.exports = {
template,
createElement,
getStaticHash,
PromiseClass,
};
export { template, createElement, getStaticHash, PromiseClass };
2 changes: 1 addition & 1 deletion packages/stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ type TCommonStoreError = {
type?: string;
};

type TCommonStore = {
export type TCommonStore = {
isCurrentLanguage(language_code: string): boolean;
error: TCommonStoreError;
has_error: boolean;
Expand Down

0 comments on commit bb07c67

Please sign in to comment.