Skip to content

Commit

Permalink
feat(typescript): convert to typescript
Browse files Browse the repository at this point in the history
update tests to tsx

match types version with dep versions

generic type

change to main/types to exports

change build to dist

change target to es6

update types/react to 18 to provide modern context type
  • Loading branch information
xAndreiLi committed Nov 9, 2024
1 parent 95702fd commit 6b834c8
Show file tree
Hide file tree
Showing 11 changed files with 9,399 additions and 8,147 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"presets": [
"amex"
"amex",
"@babel/preset-typescript"
]
}
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": ["dist/**/*"]
}
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
node-version-file: ".nvmrc"
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Release
env:
GIT_AUTHOR_EMAIL: ${{ secrets.GIT_AUTHOR_EMAIL }}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
test-results
.jest-cache
.jest-cache
dist
31 changes: 0 additions & 31 deletions __tests__/__snapshots__/index.spec.jsx.snap

This file was deleted.

31 changes: 31 additions & 0 deletions __tests__/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createSharedReactContext should not return the same context object if the key does not matches 1`] = `
<TestComponent>
<div
id="value"
>
default
</div>
</TestComponent>
`;

exports[`createSharedReactContext should return the same context object if the key matches 1`] = `
<TestComponent>
<div
id="value"
>
context value
</div>
</TestComponent>
`;

exports[`createSharedReactContext should use the same default value from first call 1`] = `
<TestComponent>
<div
id="value"
>
default
</div>
</TestComponent>
`;
29 changes: 10 additions & 19 deletions __tests__/index.spec.jsx → __tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@

import React from 'react';
import { mount } from 'enzyme';
import createSharedReactContext from '../src/index';

describe('createSharedContext', () => {
describe('createSharedReactContext', () => {
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');
createSharedContext('myCoolDefaultValue', 'sharedContext');
createSharedReactContext('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 testProvider = createSharedContext('', 'sharedContext');
const testProvider = createSharedReactContext('', 'sharedContext');
const { Provider } = testProvider;
const testConsumer = createSharedContext('', 'sharedContext');
const testConsumer = createSharedReactContext('', 'sharedContext');
const { Consumer } = testConsumer;
const mockValue = 'context value';

Expand All @@ -53,12 +50,10 @@ 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 defaultValue = 'default';
const testProvider = createSharedContext('', 'sharedContext');
const testProvider = createSharedReactContext('', 'sharedContext');
const { Provider } = testProvider;
const testConsumer = createSharedContext(defaultValue, 'someContext');
const testConsumer = createSharedReactContext(defaultValue, 'someContext');
const { Consumer } = testConsumer;
const mockValue = 'context value';

Expand All @@ -80,11 +75,9 @@ 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 defaultValue = 'default';
createSharedContext(defaultValue, 'someContext');
const testDefaultContext = createSharedContext('never see this', 'someContext');
createSharedReactContext(defaultValue, 'someContext');
const testDefaultContext = createSharedReactContext('never see this', 'someContext');
const { Consumer } = testDefaultContext;

const TestComponent = () => (
Expand All @@ -105,10 +98,8 @@ describe('createSharedContext', () => {
it('should warn and return context with React.createContext', () => {
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');

createSharedContext('default');
createSharedReactContext('default', undefined);
expect(warnSpy).toHaveBeenCalledWith('Second parameter in createSharedReactContext was not set, defaulting to React.createContext');
expect(createContextSpy).toHaveBeenCalledWith('default');
});
Expand Down
Loading

0 comments on commit 6b834c8

Please sign in to comment.