-
-
Notifications
You must be signed in to change notification settings - Fork 505
fix: item linking search results #1869
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
94b6049
fix: show at least five linking results of each type
amanharwara 4f14b4d
chore: remove unnecessary sort
amanharwara b831632
refactor: consts + concat
amanharwara fb5a8b1
refactor: check item & activeItem references directly
amanharwara befd181
Merge branch 'main' into fix/link-results
amanharwara 8dfa8c9
Merge branch 'main' into fix/link-results
amanharwara 65efc7c
Merge branch 'main' into fix/link-results
amanharwara 13d899b
Merge branch 'main' into fix/link-results
amanharwara 0f61eed
refactor: use one main loop
amanharwara 00f0b3a
Merge branch 'main' into fix/link-results
amanharwara 3f0b17c
refactor: break search results func into smaller funcs
amanharwara a4dee31
Merge branch 'main' into fix/link-results
amanharwara 993ba4b
feat: tests
amanharwara 31f4040
Merge branch 'fix/link-results' of github.com:standardnotes/app into …
amanharwara 9d4aabe
fix: tests
amanharwara 3f662df
feat: add test
amanharwara 4e13ae7
Merge branch 'main' into fix/link-results
amanharwara 70ca120
feat: add test
amanharwara 133ffe7
Merge branch 'main' into fix/link-results
amanharwara 55072a9
feat: new tests
amanharwara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,4 +22,5 @@ module.exports = { | |
| '^.+\\.(ts|tsx)?$': 'ts-jest', | ||
| '\\.svg$': 'svg-jest', | ||
| }, | ||
| testEnvironment: 'jsdom', | ||
| } | ||
221 changes: 221 additions & 0 deletions
221
packages/web/src/javascripts/Controllers/LinkingController.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { WebApplication } from '@/Application/Application' | ||
| import { | ||
| AnonymousReference, | ||
| ContentReferenceType, | ||
| ContentType, | ||
| FileItem, | ||
| FileToNoteReference, | ||
| InternalEventBus, | ||
| SNNote, | ||
| } from '@standardnotes/snjs' | ||
| import { FilesController } from './FilesController' | ||
| import { ItemListController } from './ItemList/ItemListController' | ||
| import { LinkingController } from './LinkingController' | ||
| import { NavigationController } from './Navigation/NavigationController' | ||
| import { SelectedItemsController } from './SelectedItemsController' | ||
| import { SubscriptionController } from './Subscription/SubscriptionController' | ||
|
|
||
| const createNote = (name: string, options?: Partial<SNNote>) => { | ||
| return { | ||
| title: name, | ||
| archived: false, | ||
| trashed: false, | ||
| uuid: String(Math.random()), | ||
Check failureCode scanning / CodeQL Insecure randomness
This security context depends on a cryptographically insecure random number at [Math.random()](1).
|
||
| content_type: ContentType.Note, | ||
| ...options, | ||
| } as jest.Mocked<SNNote> | ||
| } | ||
|
|
||
| const createFile = (name: string, options?: Partial<FileItem>) => { | ||
| return { | ||
| title: name, | ||
| archived: false, | ||
| trashed: false, | ||
| uuid: String(Math.random()), | ||
Check failureCode scanning / CodeQL Insecure randomness
This security context depends on a cryptographically insecure random number at [Math.random()](1).
|
||
| content_type: ContentType.File, | ||
| ...options, | ||
| } as jest.Mocked<FileItem> | ||
| } | ||
|
|
||
| describe('LinkingController', () => { | ||
| let linkingController: LinkingController | ||
| let application: WebApplication | ||
| let navigationController: NavigationController | ||
| let selectionController: SelectedItemsController | ||
| let eventBus: InternalEventBus | ||
|
|
||
| let itemListController: ItemListController | ||
| let filesController: FilesController | ||
| let subscriptionController: SubscriptionController | ||
|
|
||
| beforeEach(() => { | ||
| application = {} as jest.Mocked<WebApplication> | ||
| application.getPreference = jest.fn() | ||
| application.addSingleEventObserver = jest.fn() | ||
| application.streamItems = jest.fn() | ||
|
|
||
| navigationController = {} as jest.Mocked<NavigationController> | ||
|
|
||
| selectionController = {} as jest.Mocked<SelectedItemsController> | ||
|
|
||
| eventBus = {} as jest.Mocked<InternalEventBus> | ||
|
|
||
| itemListController = {} as jest.Mocked<ItemListController> | ||
| filesController = {} as jest.Mocked<FilesController> | ||
| subscriptionController = {} as jest.Mocked<SubscriptionController> | ||
|
|
||
| linkingController = new LinkingController(application, navigationController, selectionController, eventBus) | ||
| linkingController.setServicesPostConstruction(itemListController, filesController, subscriptionController) | ||
| }) | ||
|
|
||
| describe('isValidSearchResult', () => { | ||
| it("should not be valid result if it doesn't match query", () => { | ||
| const searchQuery = 'test' | ||
|
|
||
| const file = createFile('anotherFile') | ||
|
|
||
| const isFileValidResult = linkingController.isValidSearchResult(file, searchQuery) | ||
|
|
||
| expect(isFileValidResult).toBeFalsy() | ||
| }) | ||
|
|
||
| it('should not be valid result if item is archived or trashed', () => { | ||
| const searchQuery = 'test' | ||
|
|
||
| const archived = createFile('test', { archived: true }) | ||
|
|
||
| const trashed = createFile('test', { trashed: true }) | ||
|
|
||
| const isArchivedFileValidResult = linkingController.isValidSearchResult(archived, searchQuery) | ||
| expect(isArchivedFileValidResult).toBeFalsy() | ||
|
|
||
| const isTrashedFileValidResult = linkingController.isValidSearchResult(trashed, searchQuery) | ||
| expect(isTrashedFileValidResult).toBeFalsy() | ||
| }) | ||
|
|
||
| it('should not be valid result if result is active item', () => { | ||
| const searchQuery = 'test' | ||
|
|
||
| const activeItem = createFile('test', { uuid: 'same-uuid' }) | ||
|
|
||
| Object.defineProperty(itemListController, 'activeControllerItem', { value: activeItem }) | ||
|
|
||
| const result = createFile('test', { uuid: 'same-uuid' }) | ||
|
|
||
| const isFileValidResult = linkingController.isValidSearchResult(result, searchQuery) | ||
| expect(isFileValidResult).toBeFalsy() | ||
| }) | ||
|
|
||
| it('should be valid result if it matches query even case insensitive', () => { | ||
| const searchQuery = 'test' | ||
|
|
||
| const file = createFile('TeSt') | ||
|
|
||
| const isFileValidResult = linkingController.isValidSearchResult(file, searchQuery) | ||
|
|
||
| expect(isFileValidResult).toBeTruthy() | ||
| }) | ||
| }) | ||
|
|
||
| describe('isSearchResultAlreadyLinked', () => { | ||
| it('should be true if active item & result are same content type & active item references result', () => { | ||
| const activeItem = createFile('test', { | ||
| uuid: 'active-item', | ||
| references: [ | ||
| { | ||
| reference_type: ContentReferenceType.FileToFile, | ||
| uuid: 'result', | ||
| } as AnonymousReference, | ||
| ], | ||
| }) | ||
| const result = createFile('test', { uuid: 'result', references: [] }) | ||
|
|
||
| Object.defineProperty(itemListController, 'activeControllerItem', { value: activeItem }) | ||
|
|
||
| const isFileAlreadyLinked = linkingController.isSearchResultAlreadyLinked(result) | ||
| expect(isFileAlreadyLinked).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should be false if active item & result are same content type & result references active item', () => { | ||
| const activeItem = createFile('test', { | ||
| uuid: 'active-item', | ||
| references: [], | ||
| }) | ||
| const result = createFile('test', { | ||
| uuid: 'result', | ||
| references: [ | ||
| { | ||
| reference_type: ContentReferenceType.FileToFile, | ||
| uuid: 'active-item', | ||
| } as AnonymousReference, | ||
| ], | ||
| }) | ||
|
|
||
| Object.defineProperty(itemListController, 'activeControllerItem', { value: activeItem }) | ||
|
|
||
| const isFileAlreadyLinked = linkingController.isSearchResultAlreadyLinked(result) | ||
| expect(isFileAlreadyLinked).toBeFalsy() | ||
| }) | ||
|
|
||
| it('should be true if active item & result are different content type & result references active item', () => { | ||
| const activeNote = createNote('test', { | ||
| uuid: 'active-note', | ||
| references: [], | ||
| }) | ||
|
|
||
| const fileResult = createFile('test', { | ||
| uuid: 'file-result', | ||
| references: [ | ||
| { | ||
| reference_type: ContentReferenceType.FileToNote, | ||
| uuid: 'active-note', | ||
| } as FileToNoteReference, | ||
| ], | ||
| }) | ||
|
|
||
| Object.defineProperty(itemListController, 'activeControllerItem', { value: activeNote }) | ||
|
|
||
| const isFileResultAlreadyLinked = linkingController.isSearchResultAlreadyLinked(fileResult) | ||
| expect(isFileResultAlreadyLinked).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should be true if active item & result are different content type & active item references result', () => { | ||
| const activeFile = createNote('test', { | ||
| uuid: 'active-file', | ||
| references: [ | ||
| { | ||
| reference_type: ContentReferenceType.FileToNote, | ||
| uuid: 'note-result', | ||
| } as FileToNoteReference, | ||
| ], | ||
| }) | ||
|
|
||
| const noteResult = createFile('test', { | ||
| uuid: 'note-result', | ||
| references: [], | ||
| }) | ||
|
|
||
| Object.defineProperty(itemListController, 'activeControllerItem', { value: activeFile }) | ||
|
|
||
| const isNoteResultAlreadyLinked = linkingController.isSearchResultAlreadyLinked(noteResult) | ||
| expect(isNoteResultAlreadyLinked).toBeTruthy() | ||
| }) | ||
|
|
||
| it('should be false if active item & result are different content type & neither references the other', () => { | ||
| const activeFile = createNote('test', { | ||
| uuid: 'active-file', | ||
| references: [], | ||
| }) | ||
|
|
||
| const noteResult = createFile('test', { | ||
| uuid: 'note-result', | ||
| references: [], | ||
| }) | ||
|
|
||
| Object.defineProperty(itemListController, 'activeControllerItem', { value: activeFile }) | ||
|
|
||
| const isNoteResultAlreadyLinked = linkingController.isSearchResultAlreadyLinked(noteResult) | ||
| expect(isNoteResultAlreadyLinked).toBeFalsy() | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.