Skip to content

Commit

Permalink
Move types test to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Dec 6, 2023
1 parent 4c6669d commit df35701
Show file tree
Hide file tree
Showing 36 changed files with 163 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:
--forceExit \
--logHeapUsage \
--runInBand \
--testPathPattern packages/\|__tests__/types/
--testPathPattern=/packages/\
timeout-minutes: 10

- if: always()
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions __tests__/types/__typescript__/pass/correct-type-dir.tsx

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ module.exports = {
'<rootDir>/__tests__/html/__jest__',
'<rootDir>/__tests__/html/assets',
'<rootDir>/__tests__/setup/',
'<rootDir>/__tests__/types/__typescript__',
'<rootDir>/packages/bundle/__tests__/types/__typescript__/',
'<rootDir>/packages/core/__tests__/types/__typescript__/',
'<rootDir>/packages/directlinespeech/__tests__/utilities/',
'<rootDir>/packages/playground/',
'<rootDir>/samples/'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDirectLine } from '../../../../packages/bundle';
import { createDirectLine } from '../../../../lib/index';

// "botAgent" is a forbidden option.
createDirectLine({ botAgent: '123' });
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDirectLine } from '../../../../packages/bundle';
import { createDirectLine } from '../../../../lib/index';

// Object literal may only specify known properties, and 'invalid' does not exist in type 'Omit<CreateDirectLineOptions, "botAgent">'.
createDirectLine({ invalid: true });
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ReactWebChat from '../../../../packages/bundle';
import React from 'react';

import ReactWebChat from '../../../../lib/index';

// "dir" must be a string of "ltr' | 'rtl' | 'auto'.
<ReactWebChat dir={123} directLine={undefined} />;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStyleSet } from '../../../../packages/bundle';
import { createStyleSet } from '../../../../lib/index';

// Related to #4081.
createStyleSet({ suggestedActionsStackedOverflow: 'string' });
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hooks } from '../../../../packages/bundle';
import { hooks } from '../../../../lib/index';

const [styleOptions] = hooks.useStyleOptions();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';

import ReactWebChat from '../../../../lib/index-minimal';

// <ReactWebChat> should not contains any children.
<ReactWebChat directLine={undefined}>
<div>Hello, World</div>
</ReactWebChat>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ReactWebChat from '../../../../packages/bundle/lib/index-minimal';
import React from 'react';

import ReactWebChat from '../../../../lib/index';

// <ReactWebChat> should not contains any children.
<ReactWebChat directLine={undefined}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ReactWebChat from '../../../../packages/bundle';
import React from 'react';

import ReactWebChat from '../../../../lib/index';

// <ReactWebChat> should not contains any children.
<ReactWebChat directLine={undefined}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hooks } from '../../../../packages/bundle/lib/index-minimal';
import { hooks } from '../../../../lib/index-minimal';

const [styleOptions] = hooks.useStyleOptions();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStyleSet } from '../../../../packages/bundle/lib/index-minimal';
import { createStyleSet } from '../../../../lib/index-minimal';

// "cardEmphasisBackgroundColor" is a style options only available in full bundle.
createStyleSet({ cardEmphasisBackgroundColor: 'orange' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createStyleSet } from '../../../../lib/index';

createStyleSet({ cardEmphasisBackgroundColor: 'orange' });
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import { Components, createStore } from '../../../../packages/bundle';
import { Components, createStore } from '../../../../lib/index';

const { BasicWebChat, Composer } = Components;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

import ReactWebChat from '../../../../lib/index';

<ReactWebChat dir="ltr" directLine={undefined} />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

import { Components } from '../../../../lib/index';

const { SendTextBox } = Components;

<SendTextBox />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createDirectLine } from '../../../../lib/index';

createDirectLine({ token: 'faketoken' });
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import ReactWebChat, { createDirectLine } from '../../../../packages/bundle/lib/index-minimal';
import ReactWebChat, { createDirectLine } from '../../../../lib/index-minimal';

const directLine = createDirectLine({ token: '...' });
const styleOptions = { accent: 'black' };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import { Components, createStore } from '../../../../packages/bundle';
import { Components, createStore } from '../../../../lib/index';

const { Composer } = Components;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import ReactWebChat, { createDirectLine } from '../../../../packages/bundle';
import ReactWebChat, { createDirectLine } from '../../../../lib/index';

const directLine = createDirectLine({ token: '...' });
const styleOptions = { accent: 'black', cardEmphasisBackgroundColor: 'orange' };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import ReactWebChat, { createDirectLine, createStyleSet } from '../../../../packages/bundle/lib/index-minimal';
import ReactWebChat, { createDirectLine, createStyleSet } from '../../../../lib/index-minimal';

const directLine = createDirectLine({ token: '...' });
const styleOptions = { accent: 'black', cardEmphasisBackgroundColor: 'orange' };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import ReactWebChat, { createDirectLine } from '../../../../packages/bundle';
import ReactWebChat, { createDirectLine } from '../../../../lib/index';

const directLine = createDirectLine({ token: '...' });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hooks } from '../../../../packages/bundle';
import { hooks } from '../../../../lib/index';

const [styleOptions] = hooks.useStyleOptions();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStyleSet } from '../../../../packages/bundle';
import { createStyleSet } from '../../../../lib/index';

// Related to #4081.
createStyleSet({ suggestedActionsStackedOverflow: 'auto' });
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WebChatActivity } from '../../../../packages/core';
import type { WebChatActivity } from '../../../../lib/index';

// All activities from bot must be from server.
const activity: WebChatActivity = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WebChatActivity } from '../../../../packages/core';
import type { WebChatActivity } from '../../../../lib/index';

// All activities that failed to send, are activities that never reach the server (a.k.a. activity-in-transit).
const activity: WebChatActivity = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WebChatActivity } from '../../../../packages/core';
import type { WebChatActivity } from '../../../../lib/index';

// All activities that are sending, are activities that did not reach the server yet (a.k.a. activity-in-transit).
const activity: WebChatActivity = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WebChatActivity } from '../../../../packages/core';
import type { WebChatActivity } from '../../../../lib/index';

// All activities which are "sent", must be from server.
const activity: WebChatActivity = {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/__tests__/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strict": true
}
}
96 changes: 96 additions & 0 deletions packages/core/__tests__/types/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const { join, relative } = require('path');
const { readdir } = require('fs').promises;

const ts = require('typescript');

function compile(...filenames) {
const program = ts.createProgram(filenames, {
allowSyntheticDefaultImports: true,
jsx: 'react',
noEmit: true,
skipLibCheck: true,
strict: true
});

const emitResult = program.emit();
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
const errors = [];

allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');

errors.push(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
errors.push(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
}
});

return errors;
}

describe('compiling TypeScript files', () => {
describe('in /pass/ folder', () => {
let results;

beforeEach(async () => {
const path = join(__dirname, '__typescript__/pass/');
let files = [];

try {
files = await readdir(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

results = {};

files.forEach(file => {
const fullPath = join(path, file);

results[relative(path, fullPath)] = { errors: compile(fullPath) };
});
});

test('should pass', () => {
for (const filename of Object.keys(results)) {
expect(results).toHaveProperty([filename, 'errors'], []);
expect(results).toHaveProperty([filename, 'errors', 'length'], 0);
}
});
});

describe('in /fail-once/ folder', () => {
let results;

beforeEach(async () => {
const path = join(__dirname, '__typescript__/fail-once/');
let files = [];

try {
files = await readdir(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

results = {};

files.forEach(file => {
const fullPath = join(path, file);

results[relative(path, fullPath)] = { errors: compile(fullPath) };
});
});

test('should fail only once', () => {
for (const filename of Object.keys(results)) {
expect(results).toHaveProperty([filename, 'errors', 'length'], 1);
}
});
});
});

0 comments on commit df35701

Please sign in to comment.