Skip to content

Commit

Permalink
Merge pull request #218 from Telegram-Mini-Apps/feature/tests
Browse files Browse the repository at this point in the history
Feature/tests
  • Loading branch information
heyqbnk authored Jan 10, 2024
2 parents e619078 + b3c2387 commit 05be26e
Show file tree
Hide file tree
Showing 90 changed files with 627 additions and 497 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-falcons-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tma.js/init-data-node": minor
---

Export "parse" function.
2 changes: 1 addition & 1 deletion apps/docs/packages/node/tma-js-init-data-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ yarn add @tma.js/init-data-node
## Parsing

You can learn more about parsing utilities in [@tma.js/sdk](../typescript/tma-js-sdk/init-data/about#parsing)
documentation.
documentation. This package re-exports the `parseInitData` function as `parse`.

## Validation

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@vitest/ui": "^0.34.6",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.54.0",
"eslint": "^8.56.0",
"happy-dom": "^12.5.0",
"prettier": "^2.5.1",
"tslib": "^2.6.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/build-utils/src/createVitestConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function createVitestConfig(options: Options = {}): InlineConfig {
coverage,
} = options;
const config: InlineConfig = {
include: ['tests/**/*.ts'],
include: ['src/**/__tests__/**/*.ts'],
};

if (environment) {
Expand All @@ -23,7 +23,10 @@ export function createVitestConfig(options: Options = {}): InlineConfig {
enabled: true,
provider: 'v8',
include: ['src/**/*.ts'],
exclude: ['src/**/index.ts'],
exclude: [
'src/**/index.ts',
'src/**/__tests__/**/*',
],
...coverage,
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config-custom/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module.exports = {
'import',
],
rules: require('./rules'),
ignorePatterns: ['**/__tests__/*'],
};
9 changes: 6 additions & 3 deletions packages/init-data-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@
},
"scripts": {
"test": "vitest --run",
"lint": "eslint src/**/*",
"lint": "eslint src",
"lint:fix": "pnpm run lint --fix",
"typecheck": "tsc --noEmit",
"build": "vite build"
},
"devDependencies": {
"@types/node": "^16.0.0",
"tsconfig": "workspace:*",
"build-utils": "workspace:*",
"eslint-config-custom": "workspace:*",
"build-utils": "workspace:*"
"tsconfig": "workspace:*"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@tma.js/sdk": "workspace:^"
}
}
43 changes: 43 additions & 0 deletions packages/init-data-node/src/__tests__/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { URLSearchParams } from 'node:url';

import { expect, it } from 'vitest';

import { validate } from '../validate.js';

const sp = 'query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2';
const secretToken = '5768337691:AAH5YkoiEuPk8-FZa32hStHTqXiLPtAEhx8';

it('should throw missing hash error in case, it is not in search params', () => {
expect(() => validate('auth_date=1', secretToken))
.toThrowError('"hash" is empty or not found');
});

it('should throw an error on case, auth_date is not passed, equal to 0 or does not represent integer', () => {
expect(() => validate('auth_date=0&hash=HHH', secretToken))
.toThrowError('"auth_date" is empty or not found');
expect(() => validate('hash=HHH', secretToken))
.toThrowError('"auth_date" is empty or not found');
expect(() => validate('auth_date=AAA&hash=HHH', secretToken))
.toThrowError('"auth_date" should present integer');
});

it('should throw an error in case, parameters are expired', () => {
expect(() => validate(sp, secretToken, {
expiresIn: 1,
})).toThrowError('Init data expired');
});

it('should throw an error in case, sign is invalid', () => {
expect(() => validate(sp, `${secretToken}A`, {
expiresIn: 0,
})).toThrowError('Signature is invalid');
});

it('should correctly validate parameters in case, they are valid', () => {
expect(() => validate(sp, secretToken, { expiresIn: 0 })).not.toThrow();
expect(() => validate(new URLSearchParams(sp), secretToken, { expiresIn: 0 })).not.toThrow();
});

it('should throw an error in case, expiration time is not passed, parameters were created more than 1 day ago and already expired', () => {
expect(() => validate(sp, secretToken)).toThrow('Init data expired');
});
3 changes: 2 additions & 1 deletion packages/init-data-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './validation.js';
export { parse } from './parse.js';
export { validate, type ValidateOptions } from './validate.js';
3 changes: 3 additions & 0 deletions packages/init-data-node/src/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { parseInitData } from '@tma.js/sdk';

export { parseInitData as parse };
File renamed without changes.
46 changes: 0 additions & 46 deletions packages/init-data-node/tests/validation.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/init-data-node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": [
"src"
],
"exclude": [
"src/**/__tests__"
],
"references": [
{
"path": "./tsconfig.node.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
},
"scripts": {
"lint": "eslint src/**/*",
"lint": "eslint src",
"lint:fix": "pnpm run lint --fix",
"typecheck": "tsc --noEmit",
"build": "vite build"
Expand Down
15 changes: 8 additions & 7 deletions packages/sdk-react/src/DisplayGate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface DisplayGateProps extends PropsWithChildren {
function render(Component: ReactNode | ComponentType): ReactNode;
function render<T extends object>(Component: ReactNode | ComponentType<T>, props: T): ReactNode;
function render(Component: ReactNode | ComponentType, props = {}): ReactNode {
return typeof Component === 'function' ? <Component {...props}/> : Component;
return typeof Component === 'function' ? <Component {...props} /> : Component;
}

/**
Expand All @@ -34,9 +34,10 @@ function render(Component: ReactNode | ComponentType, props = {}): ReactNode {
export function DisplayGate(props: DisplayGateProps): ReactNode {
const { loading, initResult, error } = useSDKContext();

return initResult
? props.children
: error
? render(props.error, { error })
: render(loading ? props.loading : props.initial);
}
if (initResult) {
return props.children;
}
return error
? render(props.error, { error })
: render(loading ? props.loading : props.initial);
}
2 changes: 1 addition & 1 deletion packages/sdk-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
},
"scripts": {
"lint": "eslint src/**/*",
"lint": "eslint src",
"lint:fix": "pnpm run lint --fix",
"typecheck": "tsc --noEmit",
"build": "vite build"
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk-solid/src/DisplayGate.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
Match,
Switch,
type Component,
type ParentProps,
type JSX,
Match,
type ParentProps,
Switch,
} from 'solid-js';

import { useSDKContext } from './provider/index.js';
Expand Down Expand Up @@ -53,4 +53,4 @@ export function DisplayGate(props: DisplayGateProps) {
</Match>
</Switch>
);
}
}
2 changes: 1 addition & 1 deletion packages/sdk-solid/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export {
} from './provider/index.js';
export {
DisplayGate,
type DisplayGateProps
type DisplayGateProps,
} from './DisplayGate.js';
export type {
InitOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"scripts": {
"test": "vitest --run",
"lint": "eslint src/**/*",
"lint": "eslint src",
"lint:fix": "pnpm run lint --fix",
"typecheck": "tsc --noEmit -p tsconfig.build.json",
"build": "pnpm run build:default && pnpm run build:iife",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
setDebug,
setTargetOrigin,
targetOrigin,
} from '~/globals.js';
} from '../globals';

afterEach(() => {
vi.restoreAllMocks();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest';

import { BackButton } from '~/back-button/index.js';
import { BackButton } from '../BackButton';

describe('hide', () => {
it('should call "web_app_setup_back_button" method with "is_visible" equal to false', () => {
Expand Down Expand Up @@ -126,4 +126,4 @@ describe('supports', () => {
expect(backButton3.supports('hide')).toBe(true);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
afterAll,
} from 'vitest';

import { request, type PostEvent } from '~/bridge/index.js';
import { postEvent as globalPostEvent } from '~/bridge/methods/postEvent.js';
import { createWindow } from '~test-utils/createWindow.js';
import { dispatchWindowMessageEvent } from '~test-utils/dispatchWindowMessageEvent.js';
import { request } from '../request';
import { type PostEvent, postEvent as globalPostEvent } from '../methods/postEvent';
import { createWindow } from '../../../test-utils/createWindow';
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent';

vi.mock('~/bridge/methods/postEvent.js', async () => {
vi.mock('../methods/postEvent.js', async () => {
const { postEvent: actualPostEvent } = await vi
.importActual('../../src/bridge/methods/postEvent.js') as { postEvent: PostEvent };
.importActual('../methods/postEvent.js') as { postEvent: PostEvent };

return {
postEvent: vi.fn(actualPostEvent),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it } from 'vitest';

import { hasExternalNotify } from '~/bridge/index.js';
import { hasExternalNotify } from '../hasExternalNotify.js';

it('should return true if passed object contains path property "external.notify" and "notify" is a function property.', () => {
expect(hasExternalNotify({})).toBe(false);
Expand All @@ -12,4 +12,4 @@ it('should return true if passed object contains path property "external.notify"
},
},
})).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it } from 'vitest';

import { hasWebviewProxy } from '~/bridge/index.js';
import { hasWebviewProxy } from '../hasWebviewProxy.js';

it('should return true if passed object contains path property "TelegramWebviewProxy.postEvent" and "postEvent" is a function property.', () => {
expect(hasWebviewProxy({})).toBe(false);
Expand All @@ -12,4 +12,4 @@ it('should return true if passed object contains path property "TelegramWebviewP
},
},
})).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterAll, afterEach, expect, it, vi } from 'vitest';

import { isIframe } from '~/bridge/index.js';
import { isIframe } from '../isIframe.js';

const windowSpy = vi.spyOn(window, 'window', 'get');

Expand All @@ -27,4 +27,4 @@ it('should return true in case window.self getter threw an error', () => {
},
}) as any);
expect(isIframe()).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { createWindow, type WindowSpy } from '~test-utils/createWindow.js';
import { dispatchWindowMessageEvent } from '~test-utils/dispatchWindowMessageEvent.js';
import { createEmitter } from '~/bridge/events/createEmitter.js';
import type { MiniAppsEventParams, MiniAppsEventName } from '~/bridge/index.js';
import { createWindow, type WindowSpy } from '../../../../test-utils/createWindow';
import { dispatchWindowMessageEvent } from '../../../../test-utils/dispatchWindowMessageEvent';
import { createEmitter } from '../createEmitter';
import type { MiniAppsEventName, MiniAppsEventParams } from '../events';

type TestCase<E extends MiniAppsEventName> =
| [input: any, expected: MiniAppsEventParams<E>]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect, vi, beforeEach, afterEach, it } from 'vitest';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';

import { on, off } from '~/bridge/index.js';
import { createWindow, type WindowSpy } from '~test-utils/createWindow.js';
import { dispatchWindowMessageEvent } from '~test-utils/dispatchWindowMessageEvent.js';
import { createWindow, type WindowSpy } from '../../../../test-utils/createWindow';
import { dispatchWindowMessageEvent } from '../../../../test-utils/dispatchWindowMessageEvent';
import { off } from '../off';
import { on } from '../on';

let windowSpy: WindowSpy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect, it, vi, afterEach, beforeEach } from 'vitest';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';

import { on } from '~/bridge/index.js';
import { createWindow, type WindowSpy } from '~test-utils/createWindow.js';
import { dispatchWindowMessageEvent } from '~test-utils/dispatchWindowMessageEvent.js';
import { createWindow, type WindowSpy } from '../../../../test-utils/createWindow';
import { dispatchWindowMessageEvent } from '../../../../test-utils/dispatchWindowMessageEvent';
import { on } from '../on';

let windowSpy: WindowSpy;

Expand Down
Loading

0 comments on commit 05be26e

Please sign in to comment.