From 9a7623de1458f799baa109d0afbed08547874550 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 15 Dec 2021 13:33:02 +0100 Subject: facilitate mocking imported values --- common/indexeddb.js | 7 +++---- compute_scripts.awk | 15 +++++++++------ test/unit/test_indexeddb.py | 8 +------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/common/indexeddb.js b/common/indexeddb.js index c6302fa..96d19b7 100644 --- a/common/indexeddb.js +++ b/common/indexeddb.js @@ -92,7 +92,7 @@ async function idb_del(transaction, store_name, key) } /* Open haketilo database, asynchronously return an IDBDatabase object. */ -async function get_db(data=initial_data) +async function get_db() { if (db) return db; @@ -120,9 +120,8 @@ async function get_db(data=initial_data) for (const [store_name, key_mode] of stores) store = opened_db.createObjectStore(store_name, key_mode); - const context = make_context(store.transaction, data.files); - - await _save_items(data.resources, data.mappings, context); + const ctx = make_context(store.transaction, initial_data.files); + await _save_items(initial_data.resources, initial_data.mappings, ctx); } db = opened_db; diff --git a/compute_scripts.awk b/compute_scripts.awk index 3f237e1..3d8a5b0 100644 --- a/compute_scripts.awk +++ b/compute_scripts.awk @@ -88,12 +88,12 @@ function print_file(filename, line) { close(filename) } -function print_imports_code(filename, i, count, import_name) { +function print_imports_code(filename, declarator, i, count, import_name) { count = import_counts[filename] for (i = 1; i <= count; i++) { import_name = imports[filename,i] - printf "const %s = window.haketilo_exports.%s;\n", - import_name, import_name + printf "%s %s = window.haketilo_exports.%s;\n", + declarator, import_name, import_name } } @@ -105,8 +105,11 @@ function print_exports_code(filename, i, count, export_name) { } } -function partially_wrap_file(filename) { - print_imports_code(filename) +function partially_wrap_file(filename, declarator) { + if (!declarator) + declarator = "const" + + print_imports_code(filename, declarator) printf "\n\n" print_file(filename) @@ -197,7 +200,7 @@ BEGIN { if (compute_dependencies(root_filename) > 0) exit 1 } else if (operation == "partially_wrapped_code") { - partially_wrap_file(root_filename) + partially_wrap_file(root_filename, "let") } else if (operation == "wrapped_code") { wrap_file(root_filename) } else { diff --git a/test/unit/test_indexeddb.py b/test/unit/test_indexeddb.py index 965f318..d48946e 100644 --- a/test/unit/test_indexeddb.py +++ b/test/unit/test_indexeddb.py @@ -55,13 +55,7 @@ def test_save_remove_item(execute_in_page, indexeddb_code): """ execute_in_page(indexeddb_code) # Don't use Haketilo's default initial data. - execute_in_page( - '''{ - const _get_db = haketilodb.get; - get_db = () => _get_db({}); - haketilodb.get = get_db; - }''' - ) + execute_in_page('initial_data = {};') # Start with no database. execute_in_page( -- cgit v1.2.3