Skip to content

Handlebars + JS unit testing issue #12848

Closed

Description

@PVince81

As per OC 8 it is possible to use the Handlebars library to create JS templates.

One problem is how to store the template.
There are multiple possibilities:

  1. Store in a JS variable: a bit ugly but would work with JS unit tests

  2. Store in a PHP template (script tag): it runs but the JS unit tests have no way to read that template out of the PHP template, because there is no running server instance

  3. Store in a separate HTML file: could work, but need to find out how to load the template asynchronously. Trouble is that most of the core JS code is not async-compatible yet so loading the templates could be an issue. Also JS unit tests also need to have a way to load such files asynchronously (it should work, need to figure out the best way).

Needs further research.

Only once these questions are solved, we'll be able to enforce usage of Handlebars.
For now we could go with solution 1).

CC @DeepDiver1975

Activity

  1. PVince81 commented on Dec 15, 2014

    @PVince81
    ContributorAuthor

    The problem is not the loading itself, but how to fit the loading into the existing core apps.
    If for example we want the files app to preload the template, then we must defer the app's initialization. Might be feasible in the files app as it's more modern than the others (app.js)

    But for settings/users or others it might be more tricky / might need additional major refactoring (actually a similar problem like we met for translations)

    If we say we want to go with templates in their own files then let's do that.
    Let's make them ".html" files and store them in "templates/" ?

  2. PVince81 commented on Dec 15, 2014

    @PVince81
    ContributorAuthor

    or ".handlebars" as in your example.

    Can handlebars templates be concatenated in such scenario ?

  3. DeepDiver1975 commented on Dec 15, 2014

    @DeepDiver1975
    Contributor

    *.hbs seems to be the common file extension afaik

  4. PVince81 commented on Dec 15, 2014

    @PVince81
    ContributorAuthor

    Yes... requirejs would solve a lot of issues... We could use it in core but don't want to make it mandatory for apps...

  5. PVince81 commented on Jul 3, 2015

    @PVince81
    ContributorAuthor

    Hmm on the other hand any app that wants to switch to such templates would need changing anyway. So even if the template loading is async (ajax loading of html page or of the precompiled JS) the apps would need to be modified anyway.

    I need to look into these template formats. But if it's possible to have it as a precompiled JS file, apps could simple preload them with addScript() like we already do implicitly for translations.
    Other apps that don't mind using async load could then load these asynchronously.

  6. PVince81 commented on Jul 16, 2015

    @PVince81
    ContributorAuthor

    Another idea, just discussed: have the JS templates stored under "templates/somehandlebartemplate.html".
    The template would be included as partial inside one of the PHP templates (probably index.php).
    The JS unit test would access the template using Jasmine's built-in server, loading "templates/somehandlebartemplate.html". That loading would be async but could hopefully be done somehow before running the tests.

  7. PVince81 commented on Aug 14, 2015

    @PVince81
    ContributorAuthor

    Additional issues with CSP: #14404

  8. PVince81 commented on Aug 14, 2015

    @PVince81
    ContributorAuthor

    The goal is to be have the templates available synchronously for both the JS apps and also JS unit tests, and possibly solve the CSP issue too.

    Solution 0: (what we have now)

    Solution 1:

    • store the templates as html files "templates/something.html"
    • use PHP's include() to include them into an existing template and wrap it into a script tag (with handlebar mime type)
    • the template is available through $('#somethingTemplate') and can be compiled on the client side
    • Problem: JS unit test cannot preload the "html" file as there is no PHP when running karma. To load the template, the test files need to have extra calls to ajax-load the html templates. Not ideal.
    • Problem: breaks CSP due to eval

    I tried this locally and it is doable, but it is not ideal to have to explicitly load templates in the test files as you cannot always predict what templates a set of classes will need.

    Solution 2:

    • same as solution 1, but for the JS unit test, do some JS magic in "autotest-js.sh" or "karma.config.js" that precompiles every HTML templates with handlebars, then add them to the list of JS files to preload.
    • Problem: breaks CSP due to eval

    Solution 3:

    • precompile all HTML templates with a script or ./occ command
    • auto-include precompiled templates with \OCP\Util::addScript() so they are available directly (no async load)
    • JS unit tests can easily include the precompiled templates as they are on-disk already (no async load)
    • solves CSP issue

    This might only work with assets pipeline enabled, but even then compiled scripts won't be available for the JS unit test runner

    Having a script that devs need to run is not acceptable due to the design goal of having everything ready to go after checkout.

    And there is the question of checking in the compiled scripts or not. There is a risk of having out of date precompiled scripts. Maybe a git hook could help in some way.

    I still think that some day we'll want to have a bootstrap script, maybe a nice Makefile that will run the scripts for everything (composer, npm, translation, handlebars templates, javascript concat+minification, etc). See how it's done in the news app: https://github.com/owncloud/news/blob/master/Makefile

  9. added this to the backlog milestone on Mar 1, 2016
  10. PVince81 commented on Sep 1, 2016

    @PVince81
    ContributorAuthor

    Requires #26006 to compile JS template thanks to the new Makefile

  11. ownclouders commented on Jan 13, 2018

    @ownclouders
    Contributor

    Hey, this issue has been closed because the label status/STALE is set and there were no updates for 7 days. Feel free to reopen this issue if you deem it appropriate.

    (This is an automated comment from GitMate.io.)

  12. PVince81 commented on Jan 15, 2018

    @PVince81
    ContributorAuthor

    leaving closed in favor of #26006 which is the solution

  13. lock commented on Jul 31, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  14. locked as resolved and limited conversation to collaborators on Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions