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

[TypeScript & Module Typing] 코이(정인교) 미션 제출합니다. #17

Merged
merged 5 commits into from
Sep 29, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
50 changes: 28 additions & 22 deletions __tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
/**
* @jest-environment jsdom
*/
import _ from '../src';
import _ from "../src";

test('모듈은 기본 내보내기', () => {
expect(_).toBeTruthy();
test("모듈은 기본 내보내기", () => {
expect(_).toBeTruthy();
});

test('모듈에 포함된 함수 확인', () => {
expect(typeof _.fetch).toBe('function');
test("모듈에 포함된 함수 확인", () => {
expect(typeof _.fetch).toBe("function");
});

test('모듈에 포함된 함수 확인', () => {
expect(typeof _.pick).toBe('function');
test("모듈에 포함된 함수 확인", () => {
expect(typeof _.pick).toBe("function");
});

test('모듈에 포함된 함수 확인', () => {
expect(typeof _.omit).toBe('function');
test("모듈에 포함된 함수 확인", () => {
expect(typeof _.omit).toBe("function");
});

test('Selector 동작 확인', () => {
const divElement = document.createElement('div');
divElement.innerHTML = `<button class='test-btn'>Continue</button>`;
document.body.appendChild(divElement);
test("Selector 동작 확인", () => {
const divElement = document.createElement("div");
divElement.innerHTML = `<button class='test-btn'>Continue</button>`;
document.body.appendChild(divElement);

const buttonElement = _('button.test-btn');
expect(buttonElement).toBeTruthy();
const buttonElement = _("button.test-btn");
expect(buttonElement).toBeTruthy();

document.body.removeChild(buttonElement);
document.body.removeChild(buttonElement);
});

test('`_("").innerHTML()`~~~~', () => {});

test('`_("").show()`~~~~', () => {});

test('`_("").hidden()`~~~~', () => {});
test("show 동작 확인", () => {
const buttonElement = _("button.test-btn");
buttonElement.style.display = "hidden";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
buttonElement.style.display = "hidden";
buttonElement.style.display = "none";

buttonElement.show();
expect(buttonElement.style.display).toBe("block");
});

test('`_("").addEvent()`~~~~', () => {});
test("hidden 동작 확인", () => {
const buttonElement = _("button.test-btn");
buttonElement.style.display = "block";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show 함수를 사용해주세요 :D

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니당

buttonElement.hide();
expect(buttonElement.style.display).toBe("hidden");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(buttonElement.style.display).toBe("hidden");
expect(buttonElement.style.display).toBe("none");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 none이 맞겠네요 😅

});
34 changes: 30 additions & 4 deletions __tests__/type.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
/**
* @jest-environment jsdom
*/
import { expectType } from 'tsd';
import { expectType } from "tsd";

import _ from '../src';
import _ from "../src";

_('.button').addEvent('click', function (event) {
expectType<MouseEvent>(event);
_(".button").addEvent("click", function (event: MouseEvent) {
expectType<MouseEvent>(event);
});

expectType<boolean>(_.isNull(null));

expectType<boolean>(_.isNull(null));

expectType<boolean>(_.isNumber(3));

expectType<boolean>(_.isFunction(3));

expectType<string[]>(_.shuffle(["a", "b", "c"]));

const obj = { a: 1, b: 2, c: 3 };

expectType<Pick<typeof obj, "a">>(_.pick(obj, ["a"]));

expectType<Omit<typeof obj, "a">>(_.omit(obj, ["a"]));

const func = () => {};

expectType<typeof func>(_.debounce(func, 1000));

expectType<typeof func>(_.throttle(func, 1000));

expectType<(target: HTMLElement, event: HTMLElement) => boolean>(
_.clickOutside
);
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
verbose: true,
preset: "ts-jest",
testEnvironment: "node",
};
26 changes: 26 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
declare global {
interface HTMLElement extends CustomElement {
}
}
interface CustomElement {
innerHtml: (html: string) => string;
hide: () => void;
show: () => void;
addEvent: <T extends keyof HTMLElementEventMap>(type: T, handler: (event: HTMLElementEventMap[T]) => void) => void;
}
declare function _(selector: string): HTMLElement;
declare module _ {
function fetch<T>(url: string, config: RequestInit): Promise<T>;
function isNull<T>(value: T): boolean;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generic T를 왜 받는건가요?

function isNil<T>(value: T): boolean;
function isNumber<T>(value: T): boolean;
function isFunction<T>(value: T): boolean;
function shuffle<T>(collection: T[]): T[];
function pick<T extends Record<string, unknown>, K extends Partial<keyof T>>(obj: T, paths: K[]): Pick<T, K>;
function omit<T extends Record<string, unknown>, K extends Partial<keyof T>>(obj: T, paths: K[]): Omit<T, K>;
function memoize<T, K>(func: (...args: T[]) => K, resolver: (...args: T[]) => string): (...args: T[]) => K;
function debounce<T, K>(func: (...args: T[]) => K, wait?: number): (...args: T[]) => any;
function throttle<T, K>(func: (...args: T[]) => K, wait?: number): (...args: T[]) => any;
function clickOutside(target: HTMLElement, element: HTMLElement): boolean;
}
export default _;
Loading