Skip to content

Commit

Permalink
feat(typescript): convert to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
xAndreiLi committed Nov 8, 2024
1 parent 95702fd commit de122c6
Show file tree
Hide file tree
Showing 8 changed files with 8,832 additions and 7,780 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "amex"
"extends": "amex",
"ignorePatterns": ["build/**/*"]
}
10 changes: 5 additions & 5 deletions __tests__/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ describe('createSharedContext', () => {
it('should create context with React.createContext', () => {
const createContextSpy = jest.spyOn(React, 'createContext');
// eslint-disable-next-line unicorn/import-index, global-require
const createSharedContext = require('../index.js');
const createSharedContext = require('../build/index.js');
createSharedContext('myCoolDefaultValue', 'sharedContext');

expect(createContextSpy).toHaveBeenCalledWith('myCoolDefaultValue');
});

it('should return the same context object if the key matches', () => {
// eslint-disable-next-line unicorn/import-index, global-require
const createSharedContext = require('../index.js');
const createSharedContext = require('../build/index.js');
const testProvider = createSharedContext('', 'sharedContext');
const { Provider } = testProvider;
const testConsumer = createSharedContext('', 'sharedContext');
Expand All @@ -54,7 +54,7 @@ describe('createSharedContext', () => {

it('should not return the same context object if the key does not matches', () => {
// eslint-disable-next-line unicorn/import-index, global-require
const createSharedContext = require('../index.js');
const createSharedContext = require('../build/index.js');
const defaultValue = 'default';
const testProvider = createSharedContext('', 'sharedContext');
const { Provider } = testProvider;
Expand All @@ -81,7 +81,7 @@ describe('createSharedContext', () => {

it('should use the same default value from first call', () => {
// eslint-disable-next-line unicorn/import-index, global-require
const createSharedContext = require('../index.js');
const createSharedContext = require('../build/index.js');
const defaultValue = 'default';
createSharedContext(defaultValue, 'someContext');
const testDefaultContext = createSharedContext('never see this', 'someContext');
Expand All @@ -106,7 +106,7 @@ describe('createSharedContext', () => {
const createContextSpy = jest.spyOn(React, 'createContext');
const warnSpy = jest.spyOn(console, 'warn');
// eslint-disable-next-line unicorn/import-index, global-require
const createSharedContext = require('../index.js');
const createSharedContext = require('../build/index.js');

createSharedContext('default');
expect(warnSpy).toHaveBeenCalledWith('Second parameter in createSharedReactContext was not set, defaulting to React.createContext');
Expand Down
6 changes: 6 additions & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Context } from 'react';
export declare var contexts: {
[key: string]: Context<any>;
};
declare function createSharedReactContext(defaultValue: any, key: string): Context<any>;
export default createSharedReactContext;
21 changes: 21 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.contexts = void 0;
var react_1 = require("react");
exports.contexts = {};
function createSharedReactContext(defaultValue, key) {
if (!key) {
// eslint-disable-next-line no-console
console.warn('Second parameter in createSharedReactContext was not set, defaulting to React.createContext');
return (0, react_1.createContext)(defaultValue);
}
if (exports.contexts[key]) {
return exports.contexts[key];
}
var context = (0, react_1.createContext)(defaultValue);
exports.contexts[key] = context;
return context;
}
;
module.exports = createSharedReactContext;
exports.default = createSharedReactContext;
Loading

0 comments on commit de122c6

Please sign in to comment.