Skip to content

Commit

Permalink
Merge pull request #1503 from klembot/fix-story-list-sorting
Browse files Browse the repository at this point in the history
Fix date sorting in story list, use Lodash ES
  • Loading branch information
klembot authored Feb 29, 2024
2 parents 4c9b893 + b1965e8 commit 8278269
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/prettify-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ jobs:
- name: Prettify code
uses: creyD/[email protected]
with:
only_changed: True
github_token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
only_changed: True
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ module.exports = {
roots: ['<rootDir>/src'],
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
testEnvironment: 'jest-environment-jsdom',
// segseg is an ESM-only module.
transformIgnorePatterns: ['node_modules/(?!(segseg)/)']
// lodash and segseg are ESM-only modules.
transformIgnorePatterns: ['node_modules/(?!(lodash-es|segseg)/)']
};
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"i18next-http-backend": "^1.1.1",
"is-absolute-url": "^3.0.3",
"jsonp": "^0.2.1",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"minimist": "^1.2.8",
"mkdir-promise": "^1.0.0",
"node-fetch": "^2.6.7",
Expand Down Expand Up @@ -74,6 +74,7 @@
"@types/jest-axe": "^3.5.1",
"@types/jsonp": "^0.2.1",
"@types/lodash": "^4.14.168",
"@types/lodash-es": "^4.17.12",
"@types/minimist": "^1.2.3",
"@types/node": "^20.8.4",
"@types/node-fetch": "^2.6.1",
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/context/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import isEqual from 'lodash/isEqual';
import {isEqual} from 'lodash-es';
import {DialogsAction, DialogsState} from '../dialogs.types';

export const reducer: React.Reducer<DialogsState, DialogsAction> = (
Expand Down
9 changes: 8 additions & 1 deletion src/routes/story-list/__mocks__/story-cards.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import * as React from 'react';
import {StoryCardsProps} from '../story-cards';

export const StoryCards = () => <div data-testid="mock-story-cards" />;
export const StoryCards = ({stories}: StoryCardsProps) => (
<div data-testid="mock-story-cards">
{stories.map(story => (
<div data-testid="mock-story-card" data-id={story.id} key={story.id} />
))}
</div>
);
46 changes: 43 additions & 3 deletions src/routes/story-list/__tests__/story-list-route.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ describe('<StoryListRoute>', () => {
expect(screen.getByText('routes.storyList.noStories')).toBeInTheDocument();
});

it('sorts stories by name if the user pref is set to that', () => {
const story1 = fakeStory();
const story2 = fakeStory();

story1.name = 'a';
story1.lastUpdate = new Date('1/1/2000');
story2.name = 'b';
story2.lastUpdate = new Date('1/1/1999');
renderComponent({
prefs: {storyListSort: 'name'},
stories: [story2, story1]
});

const storyCards = screen.getAllByTestId('mock-story-card');

expect(storyCards.length).toBe(2);
expect(storyCards[0].dataset.id).toBe(story1.id);
expect(storyCards[1].dataset.id).toBe(story2.id);
});

it('sorts stories by reverse chronological edit order if the user pref is set to that', () => {
const story1 = fakeStory();
const story2 = fakeStory();

story1.name = 'b';
story1.lastUpdate = new Date('1/1/2000');
story2.name = 'a';
story2.lastUpdate = new Date('1/1/1999');
renderComponent({
prefs: {storyListSort: 'date'},
stories: [story2, story1]
});

const storyCards = screen.getAllByTestId('mock-story-card');

expect(storyCards.length).toBe(2);
expect(storyCards[0].dataset.id).toBe(story1.id);
expect(storyCards[1].dataset.id).toBe(story2.id);
});

it('displays a donation prompt if useDonationCheck() says it should be shown', () => {
useDonationCheckMock.mockReturnValue({
shouldShowDonationPrompt: () => true
Expand All @@ -69,11 +109,11 @@ describe('<StoryListRoute>', () => {
});

renderComponent();
expect(screen.queryByText('dialogs.appDonation.title')).not.toBeInTheDocument();
expect(
screen.queryByText('dialogs.appDonation.title')
).not.toBeInTheDocument();
});

it.todo('publishes a story when a story card asks to be published');

it('is accessible', async () => {
const {container} = renderComponent();

Expand Down
6 changes: 3 additions & 3 deletions src/routes/story-list/story-list-route.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sortBy from 'lodash/sortBy';
import {orderBy} from 'lodash-es';
import * as React from 'react';
import {useTranslation} from 'react-i18next';
import {MainContent} from '../../components/container/main-content';
Expand Down Expand Up @@ -43,9 +43,9 @@ export const InnerStoryListRoute: React.FC = () => {

switch (prefs.storyListSort) {
case 'date':
return sortBy(filteredStories, 'lastUpdated');
return orderBy(filteredStories, ['lastUpdate'], ['desc']);
case 'name':
return sortBy(filteredStories, 'name');
return orderBy(filteredStories, 'name');
}
}, [prefs.storyListSort, prefs.storyListTagFilter, stories]);

Expand Down
2 changes: 1 addition & 1 deletion src/store/stories/action-creators/update-passage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import escapeRegExp from 'lodash/escapeRegExp';
import {escapeRegExp} from 'lodash-es';
import {Thunk} from 'react-hook-thunk-reducer';
import {storyWithId} from '../getters';
import {Passage, StoriesAction, StoriesState, Story} from '../stories.types';
Expand Down
2 changes: 1 addition & 1 deletion src/store/stories/getters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fuse from 'fuse.js';
import uniq from 'lodash/uniq';
import {uniq} from 'lodash-es';
import {Passage, StorySearchFlags, Story} from './stories.types';
import {createRegExp} from '../../util/regexp';
import {parseLinks} from '../../util/parse-links';
Expand Down
2 changes: 1 addition & 1 deletion src/util/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import escape from 'lodash/escape';
import {escape} from 'lodash-es';
import * as publish from '../publish';
import {AppInfo} from '../app-info';
import {Passage, Story} from '../../store/stories';
Expand Down
2 changes: 1 addition & 1 deletion src/util/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// affects startup time in the Twine desktop app. This module moves data from
// the filesystem into local storage, and the app can't begin until it's done.

import defaults from 'lodash/defaults';
import {defaults} from 'lodash-es';
import uuid from 'tiny-uuid';
import {passageDefaults, storyDefaults, Passage, Story} from '../store/stories';

Expand Down
2 changes: 1 addition & 1 deletion src/util/parse-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Parses passage text for links. Optionally, it returns internal links only --
e.g. those pointing to other passages in a story, not to an external web site.
*/

import uniq from 'lodash/uniq';
import {uniq} from 'lodash-es';

// The top level regular expression to catch links -- i.e. [[link]].
const extractLinkTags = (text: string) => text.match(/\[\[.*?\]\]/g) || [];
Expand Down
2 changes: 1 addition & 1 deletion src/util/publish.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import escape from 'lodash/escape';
import {escape} from 'lodash-es';
import {Passage, Story} from '../store/stories';
import {AppInfo} from './app-info';
import {i18n} from './i18n';
Expand Down
2 changes: 1 addition & 1 deletion src/util/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import escapeRegExp from 'lodash/escapeRegExp';
import {escapeRegExp} from 'lodash-es';
import {StorySearchFlags} from '../store/stories/stories.types';

/**
Expand Down

0 comments on commit 8278269

Please sign in to comment.