index.spec.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* global describe, it, before */
  2. import chai from 'chai';
  3. import Darkmode from '../lib/darkmode-js';
  4. chai.use(require('chai-dom'));
  5. chai.expect();
  6. const expect = chai.expect;
  7. // mocking local storage
  8. global.window = {
  9. localStorage: {
  10. getItem: function(id) {
  11. return true;
  12. },
  13. setItem: function() {}
  14. },
  15. matchMedia() {
  16. return {
  17. matches: false
  18. }
  19. }
  20. };
  21. describe('Given an instance of Darkmode', () => {
  22. var darkmode = new Darkmode({
  23. bottom: '64px',
  24. right: 'unset',
  25. left: '32px',
  26. time: '0.5s',
  27. mixColor: '#fff',
  28. buttonColorDark: '#100f2c',
  29. buttonColorLight: '#fff',
  30. saveInCookies: false,
  31. label: '🌓',
  32. autoMatchOsTheme: true
  33. });
  34. describe('After creating an instance', () => {
  35. it('should not activate darkmode', () => {
  36. expect(darkmode.isActivated()).to.be.false;
  37. })
  38. it('should set the widget as inactive', () => {
  39. expect(document.getElementsByClassName('darkmode-toggle--inactive')[0]).to.not.be.undefined;
  40. })
  41. })
  42. describe('When I run toggle()', () => {
  43. it('should activate darkmode', () => {
  44. darkmode.toggle();
  45. expect(darkmode.isActivated()).to.be.true;
  46. })
  47. })
  48. describe('When I run toggle() a second time', () => {
  49. it('should disactivate darkmode', () => {
  50. darkmode.toggle();
  51. expect(darkmode.isActivated()).to.be.false;
  52. })
  53. })
  54. describe('When I run showWidget()', () => {
  55. it('the widget should have its label', () => {
  56. darkmode.showWidget();
  57. expect(document.getElementsByClassName('darkmode-toggle')[0]).to.have.html('🌓');
  58. })
  59. it('should make the widget visible', () => {
  60. darkmode.showWidget();
  61. expect(document.getElementsByClassName('darkmode-toggle')[0].classList.toString()).to.not.include('darkmode-toggle--inactive');
  62. })
  63. })
  64. });