{"id":18037560,"url":"https://github.com/jeremija/kotest","last_synced_at":"2025-03-27T09:31:45.864Z","repository":{"id":23295254,"uuid":"26654465","full_name":"jeremija/kotest","owner":"jeremija","description":"Test Knockout 3.2 components and custom binding handlers with ease","archived":false,"fork":false,"pushed_at":"2015-02-20T15:21:03.000Z","size":200,"stargazers_count":9,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-23T14:06:08.664Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeremija.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-14T19:41:34.000Z","updated_at":"2022-01-31T01:13:31.000Z","dependencies_parsed_at":"2022-07-25T12:32:49.315Z","dependency_job_id":null,"html_url":"https://github.com/jeremija/kotest","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremija%2Fkotest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremija%2Fkotest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremija%2Fkotest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremija%2Fkotest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeremija","download_url":"https://codeload.github.com/jeremija/kotest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245817483,"owners_count":20677297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T13:12:28.302Z","updated_at":"2025-03-27T09:31:45.520Z","avatar_url":"https://github.com/jeremija.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kotest\n\nTest Knockout 3.2 components and custom binding handlers with ease.\n\n# Dependencies\n\n- [KnockoutJS](http://knockoutjs.com)\n- [Mocha](http://mochajs.org)\n\nThis library supports loading via AMD or as a global object, but mocha always\nneeds to in the global scope. Knockout's alias should be set to `ko`.\n\nIn the examples below and in unit tests [expect.js](https://github.com/LearnBoost/expect.js/) and [jQuery](http://jquery.com) are used for brevity.\n\n#Usage\n\n## Testing Components\n\nAssume you have a component defined:\n\n```javascript\nfunction LoginComponent(params) {\n    this.username = ko.observable();\n    this.password = ko.observable();\n    this.onSubmit = params.onSubmit;\n}\nLoginComponent.prototype.submit = function() {\n    this.onSubmit({\n        username: this.username(),\n        password: this.password()\n    });\n};\n```\nAnd the `template` variable containing:\n```html\n\u003cform data-bind=\"submit: onSubmit\"\u003e\n    \u003cinput name=\"user\" type=\"text\" data-bind=\"value: username\"\u003e\n    \u003cinput name=\"pass\" type=\"text\" data-bind=\"value: password\"\u003e\n    \u003cinput type=\"submit\"\u003e\n\u003c/form\u003e\n```\n\nAnd you wish to test if it works correctly:\n```javascript\nvar credentials;\n\nvar params = {\n    onSubmit: function(creds) {\n        credentials = creds;\n    }\n};\n\nkotest().defineComponent('login-component', {\n    viewModel: LoginComponent,\n    template: template\n}).component('login-component', params).test('my first test', function(ctx) {\n    before(function() {\n        var $element = $(ctx.element);\n        $element.find('input[name=user]').val('john').change();\n        $element.find('input[name=pass]').val('p455w0rd').change();\n        $element.find('form').submit();\n    });\n    it('should have called params.onSubmit() with credentials', function() {\n        expect(credentials).to.eql({\n            username: 'john',\n            password: 'p455w0rd'\n        });\n    });\n});\n```\nNote that you can define multiple components with `defineComponent()`. This is\nuseful if you want to test a component which depends on other components. If\nall of your components are already loaded, you can omit the `defineComponent()`\ncall.\n\nThe `kotest()` function accepts an argument. It is the id of the test element\nin the dom to append the dynamically created elements for the test to. The\ndefault value of this argument is 'test', so you should an empty (div) element\ndefined in your main test html file.\n\n## Testing Binding Handlers\nLet's say you have a custom binding handler:\n```javascript\nvar multiplier = {\n    update: function(element, valueAccessor) {\n        element.innerHTML = ko.utils.unwrapObservable(valueAccessor()) * 2;\n    }\n};\n```\nTo test it's functionality, type:\n```javascript\nvar value = ko.observable(5);\n\nkotest().defineBinding('multiplier', multiplier)\n    .binding('multiplier', value).test('multiplier test', function(ctx) {\n        it('should make the value double', function() {\n            expect($(ctx.element).text()).to.eql(10);\n        });\n        it('should make the value double again', function() {\n            value(10);\n            expect($(ctx.element).text()).to.eql(20);\n        });\n    });\n```\nAs with the previous example, you can omit the `defineBinding()` call if your\ncustom binding handler is already set in `ko.bindingHandlers`.\n\nFor more information clone the repository and view the tests inside the\n[test/js/](test/js) folder. The examples from this file can be found in the\n[test/js/readme-examples/](test/js/readme-examples) folder. All tests can be run with `npm test`. See\nsection `Cloning and running tests` below for more details.\n\n# API Reference\nAvailable [here](API.md).\n\n# Installing\n\n```bash\nbower install kotest\n```\n\n# Cloning and running tests\n\n```bash\ngit clone https://github.com/jeremija/kotest.git\nnpm test\n```\n\nThis will also install all depenencies required for testing from the command line. You can also run these tests in browser. You will need to run HTTP server like `http-server` from the project's root directory:\n\n```\nnpm install -g http-server\nhttp-server\n```\n\nAnd then visit the [http://localhost:8080/test/test.html](http://localhost:8080/test/test.html) page in your browser. Note that `npm test` should be performed at least once beforehand because it also downloads and installs all of the necessary dependencies and writes the configuration for tests.\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremija%2Fkotest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeremija%2Fkotest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremija%2Fkotest/lists"}