How to use _setSavedState method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

status-indicator-view.js

Source: status-indicator-view.js Github

copy
1// Create a status indicator like so:
2// new StatusIndicator({el: '#pluggable-status', model: new Backbone.Model({ minActiveTime: 500 })});
3// Where minActiveTime is the minimum time the status indicator
4// You should then call onConnectedState(entity) providing a Synchrony Entity to start the ui.
5
6define('confluence-collaborative-editor-plugin/status-indicator-view', [
7    'confluence/templates',
8    'backbone',
9    'ajs',
10    'jquery',
11    'underscore'
12], function(
13    Templates,
14    Backbone,
15    AJS,
16    $,
17    _
18) {
19    'use strict';
20    return Backbone.View.extend({
21        initialize: function() {
22            this.$el.addClass('synchrony');
23
24            this.listenTo(this.model, "change:saving change:error", this.render);
25
26            // We begin in a very optimistic state.
27            this.model.set('confluenceUnreachable', false);
28            this.model.set('synchronyUnreachable', false);
29            this.model.set('tokenExpired', false);
30
31            AJS.bind("rte.heartbeat", _.bind(this.onConfluenceConnectedState, this));
32            AJS.bind("rte.safe-save.error", _.bind(this._errorPublishing, this));
33            AJS.bind("rte.preview.error", _.bind(this._errorPublishing, this));
34
35            this.model.set('connecting', true);
36            this._setProgressState();
37        },
38
39        render: function() {
40            $('.tipsy').remove(); // Here we need to remove the tooltip in case the content changes.
41            this.$el.html(Templates.CollaborativeEditor.StatusIndicator.container(this.model.attributes));
42        },
43
44        onConnectedState: function(entity) {
45            this.model.set('synchronyUnreachable', false);
46
47            if (!this.model.has('synchronyEntity')) {
48                this.model.set('synchronyEntity', entity);
49
50                var view = this;
51                this.model.get('synchronyEntity').on('update', function(event) {
52                    view._handleSynchronyUpdateEvent(event, view);
53                });
54
55                this.model.get('synchronyEntity').on('ack', function(event) {
56                    view._handleSynchronyAckEvent(event, view);
57                });
58            }
59
60            this._setSavedState();
61        },
62
63        onDisconnectedState: function() {
64            this.model.set('synchronyUnreachable', true);
65            this.model.set('connecting', true);
66            this._setErrorState();
67        },
68
69        onConfluenceConnectedState: function() {
70            if (this.model.get('confluenceUnreachable')) {
71                this.model.set('confluenceUnreachable', false);
72                this._setSavedState();
73            }
74        },
75
76        onConfluenceDisconnectedState: function() {
77            this.model.set('confluenceUnreachable', true);
78            this.model.set('connecting', true);
79            this._setErrorState();
80        },
81
82        onTokenRenewedState: function() {
83            if (this.model.get('tokenExpired')) {
84                this.model.set('tokenExpired', false);
85                this._setSavedState();
86            }
87        },
88
89        onTokenExpiredState: function() {
90            this.model.set('tokenExpired', true);
91            this.model.set('connecting', true);
92            this._setErrorState();
93        },
94
95        // Please see https://extranet.atlassian.com/display/SYNC/API+documentation for a description of the
96        // Synchrony API.
97        _handleSynchronyUpdateEvent: function(event, view) {
98            if (event.updateType === 'local') {
99                view.model.set('pendingChanges', true);
100                view._saving();
101            }
102        },
103
104        _handleSynchronyAckEvent: function(event, view) {
105            var pendingChanges = event.pending.length > 0;
106
107            view.model.set('pendingChanges', pendingChanges);
108
109            if (!pendingChanges) {
110                view.model.set('lastSavedTime', new Date().getTime());
111            }
112        },
113
114        _saving: function() {
115            var view = this;
116            var poll = function() {
117                setTimeout(function() {
118                    if (view.model.get('pendingChanges') || new Date().getTime() - view.model.get('lastSavedTime') < view.model.get('minActiveTime')) {
119                        poll();
120                    } else {
121                        view._setSavedState();
122                    }
123                }, view.model.get('minActiveTime'));
124            };
125
126            if (this.model.get('saving')) {
127                return; // We are already showing the 'saving...' message, no need to do anything.
128            }
129
130            this.model.set('connecting', false);
131            this._setProgressState();
132
133            poll();
134        },
135
136        _reachable: function() {
137            return !this.model.get('confluenceUnreachable') && !this.model.get('synchronyUnreachable') && !this.model.get('tokenExpired');
138        },
139
140        _errorPublishing: function(e, data) {
141            // Using a switch here and leaving in all the error codes so we can change this down the track easily.
142            switch (data.status) {
143                case 0: // Unreachable / no response.
144                    this.onConfluenceDisconnectedState();
145                    break;
146                case 500: // Internal server error.
147                case 503: // Request not recognised.
148                    this.onConfluenceDisconnectedState();
149                    break;
150                case 400: // Duplicate/no title etc.
151                case 403: // Not logged in.
152                case 404: // Page could not be found.
153                case 409: // Conflict.
154                case 410: // Page no longer exists/deleted.
155                case 413: // Page content too large.
156                default:
157                    break;
158            }
159        },
160
161        _setSavedState: function() {
162            if (this._reachable() && AJS.Meta.get('access-mode') !== 'READ_ONLY') {
163                this.model.set('statusMessage', this.model.get('connecting') ? AJS.I18n.getText("collab.status.connected") : AJS.I18n.getText("collab.status.saved"));
164                this.model.set('isReadyToGo', !!this.model.get('connecting'));
165                this.model.set('tooltipMessage', AJS.I18n.getText("collab.status.tooltip.success"));
166                this.model.set('connecting', false);
167                this.model.set('saving', false);
168                this.model.set('error', false);
169            } else {
170                this._setErrorState();
171            }
172        },
173
174        _setErrorState: function() {
175            var isReadWrite = AJS.Meta.get('access-mode') !== 'READ_ONLY';
176            this.model.set('statusMessage', AJS.I18n.getText("collab.status.error"));
177            this.model.set('tooltipMessage', isReadWrite ? AJS.I18n.getText("collab.status.tooltip.failure") : AJS.I18n.getText("read.only.mode.default.error.short.message"));
178            this.model.set('saving', false);
179            this.model.set('error', true);
180        },
181
182        _setProgressState: function() {
183            if (this._reachable()) {
184                this.model.set('statusMessage', this.model.get('connecting') ? AJS.I18n.getText("collab.status.connecting") : AJS.I18n.getText("collab.status.saving"));
185                this.model.set('tooltipMessage', AJS.I18n.getText("collab.status.tooltip.success"));
186                this.model.set('saving', true);
187                this.model.set('error', false);
188            } else {
189                this._setErrorState();
190            }
191        }
192    });
193});
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)