Skip to content

Commit

Permalink
🔀 Merge remote-tracking branch 'upstream/master' into docs/ds2-21-upd…
Browse files Browse the repository at this point in the history
…ates
  • Loading branch information
Shrugsy committed Jun 29, 2022
2 parents 01cb9f4 + b3d4d89 commit b959613
Show file tree
Hide file tree
Showing 23 changed files with 331 additions and 773 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

strategy:
matrix:
node-version: [12.x]
node-version: ['16.x']

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['14.x']
node: ['16.x']

steps:
- name: Checkout repo
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: ['14.x']
node: ['16.x']
steps:
- name: Checkout repo
uses: actions/checkout@v2
Expand Down Expand Up @@ -95,7 +95,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: ['14.x']
node: ['16.x']
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7']
steps:
- name: Checkout repo
Expand Down
6 changes: 3 additions & 3 deletions packages/rtk-query-codegen-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@reduxjs/toolkit": "^1.6.0",
"@types/commander": "^2.12.2",
"@types/glob-to-regexp": "^0.4.0",
"@types/jest": "^26.0.20",
"@types/jest": "^27",
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.12",
"@types/prettier": "^2.1.6",
Expand All @@ -46,11 +46,11 @@
"esbuild": "^0.13.10",
"esbuild-runner": "^2.2.1",
"husky": "^4.3.6",
"jest": "^26.6.3",
"jest": "^27",
"msw": "^0.41.1",
"openapi-types": "^9.1.0",
"pretty-quick": "^3.1.0",
"ts-jest": "^26.4.4",
"ts-jest": "^27",
"ts-node": "^10.4.0",
"yalc": "^1.0.0-pre.47"
},
Expand Down
18 changes: 5 additions & 13 deletions packages/rtk-query-codegen-openapi/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ afterEach(() => {

describe('CLI options testing', () => {
test('generation with `config.example.js`', async () => {
jest.setTimeout(10000);

const out = await cli([`./config.example.js`], __dirname);

expect(out).toEqual({
Expand All @@ -46,11 +44,9 @@ Done
});

expect(fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8')).toMatchSnapshot();
});
}, 25000);

test('paths are relative to configfile, not to cwd', async () => {
jest.setTimeout(10000);

const out = await cli([`../test/config.example.js`], path.resolve(__dirname, '../src'));

expect(out).toEqual({
Expand All @@ -62,11 +58,9 @@ Done
});

expect(fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8')).toMatchSnapshot();
});
}, 25000);

test('ts, js and json all work the same', async () => {
jest.setTimeout(25000);

await cli([`./config.example.js`], __dirname);
const fromJs = fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8');
await cli([`./config.example.ts`], __dirname);
Expand All @@ -76,12 +70,10 @@ Done

expect(fromTs).toEqual(fromJs);
expect(fromJson).toEqual(fromJs);
});
}, 25000);

test('missing parameters doesnt fail', async () => {
jest.setTimeout(25000);

const out = await cli([`./config.invalid-example.json`], __dirname);
expect(out.stderr).toContain("Error: path parameter petId does not seem to be defined in '/pet/{petId}'!")
});
expect(out.stderr).toContain("Error: path parameter petId does not seem to be defined in '/pet/{petId}'!");
}, 25000);
});
1 change: 1 addition & 0 deletions packages/toolkit/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['./jest.setup.js'],
testMatch: ['<rootDir>/src/**/*.(spec|test).[jt]s?(x)'],
moduleNameMapper: {
Expand Down
6 changes: 3 additions & 3 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^14.2.0",
"@types/convert-source-map": "^1.5.1",
"@types/jest": "^24.0.11",
"@types/jest": "^27",
"@types/json-stringify-safe": "^5.0.0",
"@types/nanoid": "^2.1.0",
"@types/node": "^10.14.4",
Expand All @@ -57,7 +57,7 @@
"eslint-plugin-react-hooks": "^4.2.0",
"fs-extra": "^9.1.0",
"invariant": "^2.2.4",
"jest": "^26.6.3",
"jest": "^27",
"json-stringify-safe": "^5.0.1",
"magic-string": "^0.25.7",
"merge-source-map": "^1.1.0",
Expand All @@ -71,7 +71,7 @@
"size-limit": "^4.11.0",
"source-map": "^0.7.3",
"terser": "^5.6.1",
"ts-jest": "^26.5.5",
"ts-jest": "^27",
"tslib": "^1.10.0",
"typescript": "~4.2.4",
"yargs": "^15.3.1"
Expand Down
10 changes: 10 additions & 0 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ export function createSlice<
if (!name) {
throw new Error('`name` is a required option for createSlice')
}

if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development'
) {
if(options.initialState === undefined) {
console.error('You must provide an `initialState` value that is not `undefined`. You may have misspelled `initialState`')
}
}

const initialState =
typeof options.initialState == 'function'
? options.initialState
Expand Down
43 changes: 22 additions & 21 deletions packages/toolkit/src/entities/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,30 @@ export function createSortedStateAdapter<T>(
return updateManyMutably([update], state)
}

// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
function takeUpdatedModel(models: T[], update: Update<T>, state: R): boolean {
if (!(update.id in state.entities)) {
return false
}

const original = state.entities[update.id]
const updated = Object.assign({}, original, update.changes)
const newKey = selectIdValue(updated, selectId)

delete state.entities[update.id]

models.push(updated)

return newKey !== update.id
}

function updateManyMutably(
updates: ReadonlyArray<Update<T>>,
state: R
): void {
const models: T[] = []
let appliedUpdates = false

for (let update of updates) {
const entity = state.entities[update.id]
if (!entity) {
continue
}

updates.forEach((update) => takeUpdatedModel(models, update, state))
appliedUpdates = true

if (models.length !== 0) {
merge(models, state)
Object.assign(entity, update.changes)
const newId = selectId(entity)
if (update.id !== newId) {
delete state.entities[update.id]
state.entities[newId] = entity
}
}

if (appliedUpdates) {
resortEntities(state)
}
}

Expand Down Expand Up @@ -139,6 +136,10 @@ export function createSortedStateAdapter<T>(
state.entities[selectId(model)] = model
})

resortEntities(state)
}

function resortEntities(state: R) {
const allEntities = Object.values(state.entities) as T[]
allEntities.sort(sort)

Expand Down
50 changes: 46 additions & 4 deletions packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,34 @@ describe('Sorted State Adapter', () => {
})
})

it('should maintain a stable sorting order when updating items', () => {
interface OrderedEntity {
id: string
order: number
ts: number
}
const sortedItemsAdapter = createEntityAdapter<OrderedEntity>({
sortComparer: (a, b) => a.order - b.order,
})
const withInitialItems = sortedItemsAdapter.setAll(
sortedItemsAdapter.getInitialState(),
[
{ id: 'A', order: 1, ts: 0 },
{ id: 'B', order: 2, ts: 0 },
{ id: 'C', order: 3, ts: 0 },
{ id: 'D', order: 3, ts: 0 },
{ id: 'E', order: 3, ts: 0 },
]
)

const updated = sortedItemsAdapter.updateOne(withInitialItems, {
id: 'C',
changes: { ts: 5 },
})

expect(updated.ids).toEqual(['A', 'B', 'C', 'D', 'E'])
})

it('should let you update many entities by id in the state', () => {
const firstChange = { title: 'Zack' }
const secondChange = { title: 'Aaron' }
Expand Down Expand Up @@ -652,12 +680,20 @@ describe('Sorted State Adapter', () => {
test('updateMany', () => {
const firstChange = { title: 'First Change' }
const secondChange = { title: 'Second Change' }
const withMany = adapter.setAll(state, [TheGreatGatsby, AClockworkOrange])
const thirdChange = { title: 'Third Change' }
const fourthChange = { author: 'Fourth Change' }
const withMany = adapter.setAll(state, [
TheGreatGatsby,
AClockworkOrange,
TheHobbit,
])

const result = createNextState(withMany, (draft) => {
adapter.updateMany(draft, [
{ id: TheGreatGatsby.id, changes: firstChange },
{ id: AClockworkOrange.id, changes: secondChange },
{ id: TheHobbit.id, changes: firstChange },
{ id: TheGreatGatsby.id, changes: secondChange },
{ id: AClockworkOrange.id, changes: thirdChange },
{ id: TheHobbit.id, changes: fourthChange },
])
})

Expand All @@ -666,14 +702,20 @@ describe('Sorted State Adapter', () => {
"entities": Object {
"aco": Object {
"id": "aco",
"title": "Second Change",
"title": "Third Change",
},
"tgg": Object {
"id": "tgg",
"title": "Second Change",
},
"th": Object {
"author": "Fourth Change",
"id": "th",
"title": "First Change",
},
},
"ids": Array [
"th",
"tgg",
"aco",
],
Expand Down
20 changes: 12 additions & 8 deletions packages/toolkit/src/entities/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { selectIdValue } from '../utils'
import { AClockworkOrange } from './fixtures/book'

describe('Entity utils', () => {
Expand All @@ -12,26 +11,29 @@ describe('Entity utils', () => {

afterEach(() => {
process.env = OLD_ENV
jest.resetAllMocks()
})

it('should not warn when key does exist', () => {
const spy = spyOn(console, 'warn')

selectIdValue(AClockworkOrange, (book) => book.id)
const { selectIdValue } = require('../utils')
const spy = jest.spyOn(console, 'warn')

selectIdValue(AClockworkOrange, (book: any) => book.id)
expect(spy).not.toHaveBeenCalled()
})

it('should warn when key does not exist in dev mode', () => {
const spy = spyOn(console, 'warn')
const { selectIdValue } = require('../utils')
const spy = jest.spyOn(console, 'warn')

selectIdValue(AClockworkOrange, (book: any) => book.foo)

expect(spy).toHaveBeenCalled()
})

it('should warn when key is undefined in dev mode', () => {
const spy = spyOn(console, 'warn')
const { selectIdValue } = require('../utils')
const spy = jest.spyOn(console, 'warn')

const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)
Expand All @@ -41,7 +43,8 @@ describe('Entity utils', () => {

it('should not warn when key does not exist in prod mode', () => {
process.env.NODE_ENV = 'production'
const spy = spyOn(console, 'warn')
const { selectIdValue } = require('../utils')
const spy = jest.spyOn(console, 'warn')

selectIdValue(AClockworkOrange, (book: any) => book.foo)

Expand All @@ -50,7 +53,8 @@ describe('Entity utils', () => {

it('should not warn when key is undefined in prod mode', () => {
process.env.NODE_ENV = 'production'
const spy = spyOn(console, 'warn')
const { selectIdValue } = require('../utils')
const spy = jest.spyOn(console, 'warn')

const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/cacheCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { waitMs } from './helpers'
import type { Middleware, Reducer } from 'redux'

beforeAll(() => {
jest.useFakeTimers()
jest.useFakeTimers('legacy')
})

const onCleanup = jest.fn()
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/cacheLifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fetchBaseQuery } from '@reduxjs/toolkit/query'
import { expectType, fakeTimerWaitFor, setupApiStore, waitMs } from './helpers'

beforeAll(() => {
jest.useFakeTimers()
jest.useFakeTimers('legacy')
})

const api = createApi({
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/tests/cleanup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function UsingAB() {
}

beforeAll(() => {
jest.useFakeTimers()
jest.useFakeTimers('legacy')
})

test('data stays in store when component stays rendered', async () => {
Expand Down
Loading

0 comments on commit b959613

Please sign in to comment.