| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
from book_list.library_data import refresh_sidebar_navigation, update_library_data |
| 6 |
from book_list.saved_virtual_libraries import save_saved_virtual_library, saved_virtual_library_item |
| 7 |
from book_list.sidebar import create_sidebar |
| 8 |
from test_book_list_helpers import with_empty_session |
| 9 |
from testing import assert_equal, assert_false, assert_true, test |
| 10 |
from utils import parse_url_params |
| 11 |
|
| 12 |
|
| 13 |
def empty_library_data(library_id='lib-a'): |
| 14 |
return { |
| 15 |
'search_result': {'library_id': library_id, 'vl': '', 'query': '', 'sort': '', 'sort_order': ''}, |
| 16 |
'sortable_fields': [], |
| 17 |
'field_metadata': {}, |
| 18 |
'metadata': {}, |
| 19 |
'virtual_libraries': {}, |
| 20 |
'book_display_fields': [], |
| 21 |
'bools_are_tristate': True, |
| 22 |
'book_details_vertical_categories': [], |
| 23 |
'fts_enabled': False, |
| 24 |
'fields_that_support_notes': [], |
| 25 |
'categories_using_hierarchy': [], |
| 26 |
'browse_fields': [], |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
@test |
| 31 |
def sidebar_navigation_refresh_contract(): |
| 32 |
calls = {'count': 0} |
| 33 |
previous = window.cs_refresh_sidebar_navigation |
| 34 |
|
| 35 |
def refresh(): |
| 36 |
calls.count += 1 |
| 37 |
|
| 38 |
try: |
| 39 |
window.cs_refresh_sidebar_navigation = refresh |
| 40 |
refresh_sidebar_navigation() |
| 41 |
assert_equal(calls.count, 1) |
| 42 |
finally: |
| 43 |
window.cs_refresh_sidebar_navigation = previous |
| 44 |
|
| 45 |
|
| 46 |
@test |
| 47 |
def update_library_data_refreshes_sidebar_navigation(): |
| 48 |
def run(sd): |
| 49 |
calls = {'count': 0} |
| 50 |
previous = window.cs_refresh_sidebar_navigation |
| 51 |
|
| 52 |
def refresh(): |
| 53 |
calls.count += 1 |
| 54 |
|
| 55 |
try: |
| 56 |
window.cs_refresh_sidebar_navigation = refresh |
| 57 |
update_library_data(empty_library_data()) |
| 58 |
assert_equal(calls.count, 1) |
| 59 |
finally: |
| 60 |
window.cs_refresh_sidebar_navigation = previous |
| 61 |
|
| 62 |
with_empty_session(run) |
| 63 |
|
| 64 |
|
| 65 |
@test |
| 66 |
def sidebar_refreshes_saved_virtual_libraries_on_url_library_change(): |
| 67 |
def run(sd): |
| 68 |
save_saved_virtual_library(saved_virtual_library_item('lib-a', 'Library A', 'tags:a')) |
| 69 |
save_saved_virtual_library(saved_virtual_library_item('lib-b', 'Library B', 'tags:b')) |
| 70 |
previous_url = window.location.href |
| 71 |
previous_update_active = window.cs_update_sidebar_active |
| 72 |
previous_refresh = window.cs_refresh_sidebar_navigation |
| 73 |
try: |
| 74 |
window.history.replaceState(None, '', '#library_id=lib-a') |
| 75 |
parse_url_params.cache = {} |
| 76 |
root, left = create_sidebar() |
| 77 |
root |
| 78 |
assert_true(left.textContent.indexOf('Library A') > -1) |
| 79 |
assert_false(left.textContent.indexOf('Library B') > -1) |
| 80 |
|
| 81 |
window.history.replaceState(None, '', '#library_id=lib-b') |
| 82 |
parse_url_params.cache = {} |
| 83 |
window.cs_update_sidebar_active() |
| 84 |
assert_true(left.textContent.indexOf('Library B') > -1) |
| 85 |
assert_false(left.textContent.indexOf('Library A') > -1) |
| 86 |
finally: |
| 87 |
window.history.replaceState(None, '', previous_url) |
| 88 |
parse_url_params.cache = {} |
| 89 |
window.cs_update_sidebar_active = previous_update_active |
| 90 |
window.cs_refresh_sidebar_navigation = previous_refresh |
| 91 |
|
| 92 |
with_empty_session(run) |